Skip to content

Commit

Permalink
fix: improvements on https configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
afnx committed Apr 24, 2024
1 parent 0758399 commit 9788ec2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
8 changes: 4 additions & 4 deletions packages/amplify-appsync-simulator/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ export class AppSyncSimulatorServer {
constructor(private config: AppSyncSimulatorServerConfig, private simulatorContext: AmplifyAppSyncSimulator) {
this._operationServer = new OperationServer(config, simulatorContext);

// Check if the SSL key path and certificate path exist and are valid
if (!config.sslKeyPath || !config.sslCertPath) {
// Check if the https configuration is not provided
if (!config.httpsConfig) {
this._httpServer = createServer(this._operationServer.app);
} else {
try {
// Read the ssl cert and key
const sslOptions = {
key: readFileSync(config.sslKeyPath),
cert: readFileSync(config.sslCertPath),
key: readFileSync(config.httpsConfig.sslKeyPath),
cert: readFileSync(config.httpsConfig.sslCertPath),
};
// Set the isHttps flag to true
this._isHttps = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ describe('getHttpsConfig', () => {
});
});

it('returns undefined paths and prints error when --https option is not followed by key and cert paths', () => {
it('returns null and prints error when --https option is not followed by key and cert paths', () => {
context.input.argv = ['--https'];

const config = getHttpsConfig(context);

expect(config).toEqual({
sslKeyPath: undefined,
sslCertPath: undefined,
});
expect(config).toEqual(null);
expect(context.print.error).toHaveBeenCalled();
});
});
14 changes: 7 additions & 7 deletions packages/amplify-util-mock/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ export class APITest {
private userOverriddenSlots: string[] = [];
private searchableTables: string[] = [];

async start(context, port: number = MOCK_API_PORT, wsPort: number = MOCK_API_PORT, sslKeyPath?: string, sslCertPath?: string) {
async start(
context,
port: number = MOCK_API_PORT,
wsPort: number = MOCK_API_PORT,
httpsConfig?: { sslKeyPath: string; sslCertPath: string },
) {
try {
context.amplify.addCleanUpTask(async (context) => {
await this.stop(context);
Expand All @@ -72,8 +77,7 @@ export class APITest {
this.appSyncSimulator = new AmplifyAppSyncSimulator({
port,
wsPort,
sslKeyPath,
sslCertPath,
httpsConfig: httpsConfig,
});
await this.appSyncSimulator.start();
await this.resolverOverrideManager.start();
Expand All @@ -89,10 +93,6 @@ export class APITest {
await this.generateTestFrontendExports(context);
await this.generateCode(context, appSyncConfig);

if (this.appSyncSimulator.isHttps) {
context.print.success('\nHTTPS enabled successfully.\n');
}

context.print.info(`AppSync Mock endpoint is running at ${this.appSyncSimulator.url}`);
context.print.info(`GraphiQL IDE is available for local testing at ${this.appSyncSimulator.localhostUrl}`);
await this.startDDBListeners(context, appSyncConfig, false);
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-util-mock/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function start(context) {
const mockConfig = await getMockConfig(context);
const httpsConfig = getHttpsConfig(context);

await testApi.start(context, mockConfig.graphqlPort, mockConfig.graphqlPort, httpsConfig.sslKeyPath, httpsConfig.sslCertPath);
await testApi.start(context, mockConfig.graphqlPort, mockConfig.graphqlPort, httpsConfig);
} catch (e) {
console.log(e);
// Sending term signal so we clean up after ourselves
Expand Down
16 changes: 9 additions & 7 deletions packages/amplify-util-mock/src/utils/get-https-config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export function getHttpsConfig(context): { sslKeyPath: string | undefined; sslCertPath: string | undefined } {
const paths: { sslKeyPath: string | undefined; sslCertPath: string | undefined } = { sslKeyPath: undefined, sslCertPath: undefined };

export function getHttpsConfig(context): { sslKeyPath: string; sslCertPath: string } | null {
if (!context.input || !context.input.argv) {
return paths;
return null;
}

const argv = context.input.argv;
Expand All @@ -12,8 +10,12 @@ export function getHttpsConfig(context): { sslKeyPath: string | undefined; sslCe
if (httpsIndex < argv.length - 2) {
const keyPath = argv[httpsIndex + 1];
const certPath = argv[httpsIndex + 2];
paths.sslKeyPath = keyPath;
paths.sslCertPath = certPath;
if (typeof keyPath === 'string' && typeof certPath === 'string') {
return { sslKeyPath: keyPath, sslCertPath: certPath };
} else {
context.print.error('\nThe provided paths for the SSL key and certificate are not valid.\n');
context.print.error('Please ensure you have entered the correct paths.\n');
}
} else {
context.print.error('\nThe --https option must be followed by the path to the SSL key and the path to the SSL certificate.\n');
context.print.error('Example: amplify mock api --https /path/to/key /path/to/cert\n');
Expand All @@ -23,5 +25,5 @@ export function getHttpsConfig(context): { sslKeyPath: string | undefined; sslCe
}
}

return paths;
return null;
}

0 comments on commit 9788ec2

Please sign in to comment.