-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Expose connection params (#1139)
Needed in relaycorp/relaynet-gateway-android#726
- Loading branch information
Showing
20 changed files
with
340 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,28 @@ | ||
import { CertificationPath, issueGatewayCertificate } from '@relaycorp/relaynet-core'; | ||
import { MongoCertificateStore } from '@relaycorp/awala-keystore-mongodb'; | ||
import { addDays } from 'date-fns'; | ||
import { Connection } from 'mongoose'; | ||
import type { Connection } from 'mongoose'; | ||
|
||
import { createMongooseConnectionFromEnv } from '../backingServices/mongo'; | ||
import { initPrivateKeyStore } from '../backingServices/keystore'; | ||
import { CERTIFICATE_TTL_DAYS } from '../pki'; | ||
import { Config, ConfigKey } from '../utilities/config'; | ||
import { configureExitHandling } from '../utilities/exitHandling'; | ||
import { makeLogger } from '../utilities/logging'; | ||
import { InternetGatewayManager } from '../node/InternetGatewayManager'; | ||
|
||
const LOGGER = makeLogger(); | ||
configureExitHandling(LOGGER); | ||
|
||
async function main(): Promise<void> { | ||
const connection = await createMongooseConnectionFromEnv(); | ||
const connection = createMongooseConnectionFromEnv(); | ||
try { | ||
const certificateStore = new MongoCertificateStore(connection); | ||
|
||
const config = new Config(connection); | ||
const currentId = await config.get(ConfigKey.CURRENT_ID); | ||
if (currentId) { | ||
LOGGER.info({ id: currentId }, `Gateway key pair already exists`); | ||
} else { | ||
await generateKeyPair(certificateStore, config, connection); | ||
} | ||
await bootstrap(connection); | ||
} finally { | ||
await connection.close(); | ||
} | ||
} | ||
|
||
async function generateKeyPair( | ||
certificateStore: MongoCertificateStore, | ||
config: Config, | ||
connection: Connection, | ||
): Promise<void> { | ||
const privateKeyStore = initPrivateKeyStore(connection); | ||
const { id, privateKey, publicKey } = await privateKeyStore.generateIdentityKeyPair(); | ||
|
||
const gatewayCertificate = await issueGatewayCertificate({ | ||
issuerPrivateKey: privateKey, | ||
subjectPublicKey: publicKey, | ||
validityEndDate: addDays(new Date(), CERTIFICATE_TTL_DAYS), | ||
}); | ||
await certificateStore.save(new CertificationPath(gatewayCertificate, []), id); | ||
|
||
await config.set(ConfigKey.CURRENT_ID, id); | ||
LOGGER.info('Done'); | ||
} | ||
|
||
LOGGER.info({ id }, 'Identity key pair was successfully generated'); | ||
async function bootstrap(connection: Connection): Promise<void> { | ||
const manager = await InternetGatewayManager.init(connection); | ||
const gateway = await manager.getOrCreateCurrent(); | ||
await gateway.makeInitialSessionKeyIfMissing(); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.