Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: shopify-app-session-storage-[mysql,postgresql] to respect SSL DB url param #437

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/red-weeks-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/shopify-app-session-storage-mysql': patch
'@shopify/shopify-app-session-storage-postgresql': patch
---

Update adds support for SSL DB url connection param
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,21 @@ describe('MySQLSessionStorage', () => {
expect(await storage.storeSession(session)).toBeTruthy();
await storage.disconnect();
});

it(`can successfully connect with a url string and ssl param`, async () => {
const _dbURL = new URL(dbURL);
_dbURL.searchParams.append('ssl', 'false');
const storage = new MySQLSessionStorage(_dbURL.toString());
await storage.ready;
const session = new Session({
id: '456',
shop: 'test-shop.myshopify.com',
state: 'test-state',
isOnline: false,
scope: 'fake_scope',
});

expect(await storage.storeSession(session)).toBeTruthy();
await storage.disconnect();
});
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DATABASE_URL=mysql://root:password@localhost:3306/db?ssl={"rejectUnauthorized":true}
(this.dbUrl.searchParams.get('ssl') !== 'false').toString()

Considering the above database connection URL this.dbUrl.searchParams.get('ssl') will return either '{"rejectUnauthorized":true}' as a string or null if not found. I think the !=='false' check will not work here.

I would suggest the following solution :

private async init(): Promise<void> {
    const sslConfig = this.dbUrl.searchParams.get('ssl');
    this.pool = await mysql.createPool({
      connectionLimit: this.connectionPoolLimit,
      host: this.dbUrl.hostname,
      user: decodeURIComponent(this.dbUrl.username),
      password: decodeURIComponent(this.dbUrl.password),
      database: this.getDatabase(),
      port: Number(this.dbUrl.port),
      ssl : sslConfig ? JSON.parse(sslConfig) : undefined
    });
  }

Ref of ssl types:

ssl?: string | SslOptions;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maruffahmed good catch, will update the pull request.

Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class MySqlConnection implements RdbmsConnection {
password: decodeURIComponent(this.dbUrl.password),
database: this.getDatabase(),
port: Number(this.dbUrl.port),
ssl: (this.dbUrl.searchParams.get('ssl') !== 'false').toString(),
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,21 @@ describe('PostgreSQLSessionStorage', () => {
expect(await storage.storeSession(session)).toBeTruthy();
await storage.disconnect();
});

it(`can successfully connect with a url string and ssl param`, async () => {
const _dbURL = new URL(dbURL);
_dbURL.searchParams.append('ssl', 'false');
const storage = new PostgreSQLSessionStorage(_dbURL.toString());
await storage.ready;
const session = new Session({
id: '456',
shop: 'test-shop.myshopify.com',
state: 'test-state',
isOnline: false,
scope: 'fake_scope',
});

expect(await storage.storeSession(session)).toBeTruthy();
await storage.disconnect();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class PostgresConnection implements RdbmsConnection {
password: decodeURIComponent(this.dbUrl.password),
database: this.getDatabase(),
port: Number(this.dbUrl.port),
ssl: this.dbUrl.searchParams.get('ssl') !== 'false',
});
}
}