Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hdfc-common-utility
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Roshan Suvarnkar
hdfc-common-utility
Commits
d9c674d1
Commit
d9c674d1
authored
1 month ago
by
roshan
Browse files
Options
Downloads
Patches
Plain Diff
gcp secrete key
parent
92255603
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/utils/gcp-secretes.utils.ts
+13
-11
13 additions, 11 deletions
src/utils/gcp-secretes.utils.ts
with
13 additions
and
11 deletions
src/utils/gcp-secretes.utils.ts
+
13
−
11
View file @
d9c674d1
...
...
@@ -3,14 +3,16 @@ import { SecretManagerServiceClient } from '@google-cloud/secret-manager';
import
*
as
fs
from
'
fs
'
;
import
*
as
path
from
'
path
'
;
async
function
createSecretManagerClient
():
Promise
<
{
export
async
function
createSecretManagerClient
():
Promise
<
{
client
:
SecretManagerServiceClient
;
projectId
:
string
;
}
>
{
try
{
const
keyPath
=
path
.
resolve
(
__dirname
,
'
gcp-key.json
'
);
if
(
fs
.
existsSync
(
keyPath
))
{
const
keyPath
=
path
.
resolve
(
process
.
cwd
(),
'
gcp-key.json
'
);
// or wherever it is
const
credentials
=
JSON
.
parse
(
fs
.
readFileSync
(
keyPath
,
'
utf8
'
));
if
(
credentials
)
{
const
client
=
new
SecretManagerServiceClient
({
credentials
:
{
client_email
:
credentials
.
client_email
,
...
...
@@ -18,19 +20,21 @@ async function createSecretManagerClient(): Promise<{
},
projectId
:
credentials
.
project_id
,
});
console
.
log
(
'
Using service account from local JSON file
'
);
console
.
log
(
'
Using credentials object from memory
'
);
return
{
client
,
projectId
:
credentials
.
project_id
};
}
else
{
throw
new
Error
(
'
No local key file found
'
);
}
throw
new
Error
(
'
No credentials set; falling back...
'
);
}
catch
{
const
client
=
new
SecretManagerServiceClient
();
const
projectId
=
await
client
.
getProjectId
();
console
.
log
(
'
Using
Google
Application Default Credentials
'
);
console
.
log
(
'
Using Application Default Credentials
'
);
return
{
client
,
projectId
};
}
}
function
toEnvKey
(
secretId
:
string
):
string
{
return
secretId
.
toUpperCase
().
replace
(
/
[^
A-Z0-9_
]
/gi
,
'
_
'
);
}
...
...
@@ -77,6 +81,4 @@ async function fetchAllSecretsAndWriteEnv() {
console
.
log
(
`All secrets written to
${
envFilePath
}
`
);
}
fetchAllSecretsAndWriteEnv
().
catch
(
err
=>
{
console
.
error
(
'
Error fetching secrets:
'
,
err
);
});
export
{
fetchAllSecretsAndWriteEnv
};
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment