Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
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
Bob Public Utils
bobgroup-go-utils
Commits
afc4645d
Commit
afc4645d
authored
2 years ago
by
Billy Griffiths
Browse files
Options
Downloads
Patches
Plain Diff
ADHOC - reuse the secretManagerSession if a connection already exists
parent
badb9ddd
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!35
ADHOC - Expand secrets manager to support create and delete
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
secrets_manager/secrets_manager.go
+20
-13
20 additions, 13 deletions
secrets_manager/secrets_manager.go
with
20 additions
and
13 deletions
secrets_manager/secrets_manager.go
+
20
−
13
View file @
afc4645d
...
@@ -36,6 +36,8 @@ var (
...
@@ -36,6 +36,8 @@ var (
secretManagerRegion
=
"af-south-1"
secretManagerRegion
=
"af-south-1"
)
)
var
secretManagerSession
*
secretsmanager
.
SecretsManager
func
GetDatabaseCredentials
(
secretID
string
,
isDebug
bool
)
(
DatabaseCredentials
,
error
)
{
func
GetDatabaseCredentials
(
secretID
string
,
isDebug
bool
)
(
DatabaseCredentials
,
error
)
{
secret
,
_
:=
GetSecret
(
secretID
,
isDebug
)
secret
,
_
:=
GetSecret
(
secretID
,
isDebug
)
var
credentials
DatabaseCredentials
var
credentials
DatabaseCredentials
...
@@ -56,11 +58,16 @@ func GetS3UploadCredentials(secretID string, isDebug bool) (*credentials2.Creden
...
@@ -56,11 +58,16 @@ func GetS3UploadCredentials(secretID string, isDebug bool) (*credentials2.Creden
return
credentials2
.
NewStaticCredentials
(
credentials
.
AccessKeyID
,
credentials
.
SecretKey
,
""
),
nil
return
credentials2
.
NewStaticCredentials
(
credentials
.
AccessKeyID
,
credentials
.
SecretKey
,
""
),
nil
}
}
// createClient Instantiates a new Secrets Manager client
// getSecretManagerSession Instantiates a new Secrets Manager client session
func
createClient
(
isDebug
bool
)
(
svc
*
secretsmanager
.
SecretsManager
,
err
error
)
{
func
getSecretManagerSession
(
isDebug
bool
)
(
err
error
)
{
if
secretManagerSession
!=
nil
{
return
nil
}
logs
.
Info
(
"Creating a new Secrets Manager session"
)
awsSession
,
err
:=
session
.
NewSession
()
awsSession
,
err
:=
session
.
NewSession
()
if
err
!=
nil
{
if
err
!=
nil
{
return
svc
,
err
return
err
}
}
// Get local config
// Get local config
...
@@ -76,14 +83,14 @@ func createClient(isDebug bool) (svc *secretsmanager.SecretsManager, err error)
...
@@ -76,14 +83,14 @@ func createClient(isDebug bool) (svc *secretsmanager.SecretsManager, err error)
},
},
})
})
if
err
!=
nil
{
if
err
!=
nil
{
return
svc
,
err
return
err
}
}
}
}
// Create a Secrets Manager client
// Create a Secrets Manager client
session
s
vc
=
secretsmanager
.
New
(
awsSession
,
aws
.
NewConfig
()
.
WithRegion
(
secretManagerRegion
))
s
ecretManagerSession
=
secretsmanager
.
New
(
awsSession
,
aws
.
NewConfig
()
.
WithRegion
(
secretManagerRegion
))
return
svc
,
nil
return
nil
}
}
// logError Logs any errors returned by the Secrets Manager client
// logError Logs any errors returned by the Secrets Manager client
...
@@ -108,7 +115,7 @@ func GetSecret(secretID string, isDebug bool) (string, string) {
...
@@ -108,7 +115,7 @@ func GetSecret(secretID string, isDebug bool) (string, string) {
}
}
// Create a Secrets Manager client
// Create a Secrets Manager client
svc
,
err
:
=
createClient
(
isDebug
)
err
=
getSecretManagerSession
(
isDebug
)
if
err
!=
nil
{
if
err
!=
nil
{
logs
.
Info
(
"Could not create client: %+v"
,
err
)
logs
.
Info
(
"Could not create client: %+v"
,
err
)
return
""
,
""
return
""
,
""
...
@@ -120,7 +127,7 @@ func GetSecret(secretID string, isDebug bool) (string, string) {
...
@@ -120,7 +127,7 @@ func GetSecret(secretID string, isDebug bool) (string, string) {
VersionStage
:
aws
.
String
(
"AWSCURRENT"
),
// VersionStage defaults to AWSCURRENT if unspecified
VersionStage
:
aws
.
String
(
"AWSCURRENT"
),
// VersionStage defaults to AWSCURRENT if unspecified
}
}
result
,
err
:=
s
vc
.
GetSecretValue
(
input
)
result
,
err
:=
s
ecretManagerSession
.
GetSecretValue
(
input
)
if
err
!=
nil
{
if
err
!=
nil
{
logError
(
err
)
logError
(
err
)
return
""
,
""
return
""
,
""
...
@@ -147,7 +154,7 @@ func GetSecret(secretID string, isDebug bool) (string, string) {
...
@@ -147,7 +154,7 @@ func GetSecret(secretID string, isDebug bool) (string, string) {
// CreateSecret Creates a JSON marshaled "string secret" (can be expanded to cater for binary secrets should the need arise)
// CreateSecret Creates a JSON marshaled "string secret" (can be expanded to cater for binary secrets should the need arise)
func
CreateSecret
(
secretID
string
,
secret
any
,
isDebug
bool
)
(
awsSecretID
string
,
err
error
)
{
func
CreateSecret
(
secretID
string
,
secret
any
,
isDebug
bool
)
(
awsSecretID
string
,
err
error
)
{
// Create a Secrets Manager client
// Create a Secrets Manager client
svc
,
err
:
=
createClient
(
isDebug
)
err
=
getSecretManagerSession
(
isDebug
)
if
err
!=
nil
{
if
err
!=
nil
{
logs
.
Info
(
"Could not create client: %+v"
,
err
)
logs
.
Info
(
"Could not create client: %+v"
,
err
)
return
""
,
err
return
""
,
err
...
@@ -164,7 +171,7 @@ func CreateSecret(secretID string, secret any, isDebug bool) (awsSecretID string
...
@@ -164,7 +171,7 @@ func CreateSecret(secretID string, secret any, isDebug bool) (awsSecretID string
SecretString
:
aws
.
String
(
string
(
secretStr
)),
SecretString
:
aws
.
String
(
string
(
secretStr
)),
}
}
result
,
err
:=
s
vc
.
CreateSecret
(
input
)
result
,
err
:=
s
ecretManagerSession
.
CreateSecret
(
input
)
if
err
!=
nil
{
if
err
!=
nil
{
logError
(
err
)
logError
(
err
)
return
""
,
err
return
""
,
err
...
@@ -175,7 +182,7 @@ func CreateSecret(secretID string, secret any, isDebug bool) (awsSecretID string
...
@@ -175,7 +182,7 @@ func CreateSecret(secretID string, secret any, isDebug bool) (awsSecretID string
func
DeleteSecret
(
secretID
string
,
forceWithoutRecovery
bool
,
isDebug
bool
)
error
{
func
DeleteSecret
(
secretID
string
,
forceWithoutRecovery
bool
,
isDebug
bool
)
error
{
// Create a Secrets Manager client
// Create a Secrets Manager client
svc
,
err
:=
createClient
(
isDebug
)
err
:=
getSecretManagerSession
(
isDebug
)
if
err
!=
nil
{
if
err
!=
nil
{
logs
.
Info
(
"Could not create client: %+v"
,
err
)
logs
.
Info
(
"Could not create client: %+v"
,
err
)
return
err
return
err
...
@@ -187,7 +194,7 @@ func DeleteSecret(secretID string, forceWithoutRecovery bool, isDebug bool) erro
...
@@ -187,7 +194,7 @@ func DeleteSecret(secretID string, forceWithoutRecovery bool, isDebug bool) erro
ForceDeleteWithoutRecovery
:
aws
.
Bool
(
forceWithoutRecovery
),
ForceDeleteWithoutRecovery
:
aws
.
Bool
(
forceWithoutRecovery
),
}
}
_
,
err
=
s
vc
.
DeleteSecret
(
input
)
_
,
err
=
s
ecretManagerSession
.
DeleteSecret
(
input
)
if
err
!=
nil
{
if
err
!=
nil
{
logError
(
err
)
logError
(
err
)
return
err
return
err
...
...
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