Skip to content

Commit eff522c

Browse files
committed
PR comments
1 parent 1bdc6e0 commit eff522c

File tree

9 files changed

+29
-21
lines changed

9 files changed

+29
-21
lines changed

run/idp-sql/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ For more details on how to work with this sample read the [Google Cloud Run Node
1818
* **knex** + **pg**: A postgreSQL query builder library
1919
* **handlebars.js**: Template engine
2020
* **google-auth-library-nodejs**: Access [compute metadata server](https://cloud.google.com/compute/docs/storing-retrieving-metadata) for project ID
21+
* **Firebase JavaScript SDK**: client-side library for authentication flow
2122

2223
## Environment Variables
2324

run/idp-sql/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ app.get('/', getTrace, async (req, res) => {
7777
} catch (err) {
7878
const message = "Error while connecting to the Cloud SQL database. " +
7979
"Check that your username and password are correct, that the Cloud SQL " +
80-
"proxy is running (locally), and that the database/table exits and is " +
80+
"proxy is running (locally), and that the database/table exists and is " +
8181
`ready for use: ${err}`;
8282
logger.error({message, traceId: req.traceId});
8383
res

run/idp-sql/app.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"name": "idp-sql",
33
"env": {
44
"DB_PASSWORD": {
5-
"description": "postgreSQL password",
6-
"value": "password1234"
5+
"description": "postgreSQL password"
76
},
87
"CLOUD_SQL_INSTANCE_NAME": {
98
"description": "Cloud SQL instance name",

run/idp-sql/cloud-run-button-script.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
export _SECRET_NAME="vote-sql-secrets"
16-
export _SERVICE_ACCOUNT="vote-indentity"
15+
export SECRET_NAME="idp-sql-secrets"
16+
export SERVICE_ACCOUNT="idp-sql-indentity"
1717

1818
gcloud config set project $GOOGLE_CLOUD_PROJECT
1919

@@ -22,7 +22,7 @@ gcloud services enable sqladmin.googleapis.com secretmanager.googleapis.com
2222

2323
gcloud sql instances describe ${CLOUD_SQL_INSTANCE_NAME}
2424
if [ $? -eq 1 ]; then
25-
echo "Create Cloud SQL instance with postgreSQL database ..."
25+
echo "Create Cloud SQL instance with postgreSQL database (this might take a few minutes)..."
2626
gcloud sql instances create ${CLOUD_SQL_INSTANCE_NAME} \
2727
--database-version=POSTGRES_12 \
2828
--region=${GOOGLE_CLOUD_REGION} \
@@ -36,29 +36,29 @@ sed -i "s/REGION/$GOOGLE_CLOUD_REGION/" postgres-secrets.json
3636
sed -i "s/PASSWORD_SECRET/$DB_PASSWORD/" postgres-secrets.json
3737
sed -i "s/INSTANCE/$CLOUD_SQL_INSTANCE_NAME/" postgres-secrets.json
3838

39-
gcloud secrets describe ${_SECRET_NAME}
39+
gcloud secrets describe ${SECRET_NAME}
4040
if [ $? -eq 1 ]; then
4141
echo "Creating secret ..."
42-
gcloud secrets create ${_SECRET_NAME} \
42+
gcloud secrets create ${SECRET_NAME} \
4343
--replication-policy="automatic"
4444
fi
4545
echo "Adding secret version ..."
46-
gcloud secrets versions add ${_SECRET_NAME} --data-file=postgres-secrets.json
46+
gcloud secrets versions add ${SECRET_NAME} --data-file=postgres-secrets.json
4747

4848
# Create service account
49-
gcloud iam service-accounts create ${_SERVICE_ACCOUNT}
49+
gcloud iam service-accounts create ${SERVICE_ACCOUNT}
5050
# Allow service account to access secret
51-
gcloud secrets add-iam-policy-binding ${_SECRET_NAME} \
52-
--member serviceAccount:${_SERVICE_ACCOUNT}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com \
51+
gcloud secrets add-iam-policy-binding ${SECRET_NAME} \
52+
--member serviceAccount:${SERVICE_ACCOUNT}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com \
5353
--role roles/secretmanager.secretAccessor
5454
# Allow service account to access Cloud SQL
5555
gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT \
56-
--member serviceAccount:${_SERVICE_ACCOUNT}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com \
56+
--member serviceAccount:${SERVICE_ACCOUNT}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com \
5757
--role roles/cloudsql.client
5858

5959
gcloud run services update ${K_SERVICE} \
6060
--platform managed \
6161
--region ${GOOGLE_CLOUD_REGION} \
62-
--service-account ${_SERVICE_ACCOUNT}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com \
62+
--service-account ${SERVICE_ACCOUNT}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com \
6363
--add-cloudsql-instances ${GOOGLE_CLOUD_PROJECT}:${GOOGLE_CLOUD_REGION}:${CLOUD_SQL_INSTANCE_NAME} \
64-
--set-env-vars CLOUD_SQL_CREDENTIALS_SECRET=projects/${GOOGLE_CLOUD_PROJECT}/secrets/${_SECRET_NAME}/versions/latest
64+
--update-env-vars CLOUD_SQL_CREDENTIALS_SECRET=projects/${GOOGLE_CLOUD_PROJECT}/secrets/${SECRET_NAME}/versions/latest

run/idp-sql/cloud-sql.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ const config = {
3434
}
3535

3636
// [START run_user_auth_sql_connect]
37+
/**
38+
* Connect to the Cloud SQL instance through UNIX Sockets
39+
*
40+
* @param {object} credConfig The Cloud SQL connection configuration from Secret Manager
41+
* @returns {object} Knex's PostgreSQL client
42+
*/
3743
const connectWithUnixSockets = async (credConfig) => {
3844
const dbSocketPath = process.env.DB_SOCKET_PATH || "/cloudsql"
3945
// Establish a connection to the database

run/idp-sql/middleware.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
const admin = require('firebase-admin');
16-
const { logger } = require('./logging');
15+
const { logger } = require('./logging'); // Import winston logger instance
1716

1817
// [START run_user_auth_jwt]
18+
const admin = require('firebase-admin');
1919
// Extract and verify Id Token from header
2020
const authenticateJWT = (req, res, next) => {
2121
const authHeader = req.headers.authorization;

run/idp-sql/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@google-cloud/secret-manager": "^3.1.0",
2222
"express": "^4.16.2",
2323
"firebase-admin": "^9.1.0",
24+
"gcp-metadata": "^4.2.0",
2425
"google-auth-library": "^6.1.1",
2526
"handlebars": "^4.7.6",
2627
"knex": "^0.21.0",

run/idp-sql/secrets.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
// limitations under the License.
1414

1515
const { logger } = require('./logging');
16-
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
1716

1817
// CLOUD_SQL_CREDENTIALS_SECRET is the resource ID of the secret, passed in by environment variable.
1918
// Format: projects/PROJECT_ID/secrets/SECRET_ID/versions/VERSION
2019
const {CLOUD_SQL_CREDENTIALS_SECRET} = process.env;
2120

22-
let client;
2321

2422
// [START run_user_auth_secrets]
23+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
24+
let client;
25+
2526
async function getSecrets(secretName) {
2627
if (!client) client = new SecretManagerServiceClient();
2728
try {
@@ -34,7 +35,7 @@ async function getSecrets(secretName) {
3435
}
3536
// [END run_user_auth_secrets]
3637

37-
// Load the secret from Secret Manager
38+
// Load the Cloud SQL config from Secret Manager
3839
async function getCredConfig() {
3940
if (CLOUD_SQL_CREDENTIALS_SECRET) {
4041
const secrets = await getSecrets(CLOUD_SQL_CREDENTIALS_SECRET);

run/idp-sql/static/firebase.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async function vote(team) {
6262
'Content-Type': 'application/x-www-form-urlencoded',
6363
'Authorization': `Bearer ${token}`
6464
},
65-
body: 'team=' + team,
65+
body: 'team=' + team, // send application data (vote)
6666
});
6767
if (response.ok) {
6868
const text = await response.text();

0 commit comments

Comments
 (0)