diff --git a/README.md b/README.md index 079c04e..80c063f 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,8 @@ You may use reporting and generation of CDK artifacts for Cloud Formation deploy s := NewSsmSerializer("dev", "test-service") objs, json, err := s.ReportWithOpts(&ctx, NoFilter, true) ``` -The above will cereate a _JSON_ report format that can be used to generate CDK classes. Example output +The above will cereate a _JSON_ report format that can be used to generate CDK classes. Example output for [ssm-cdk-generator](https://www.npmjs.com/package/ssm-cdk-generator) + ```typescript import * as cdk from '@aws-cdk/core'; import * as asm from '@aws-cdk/aws-secretsmanager'; @@ -130,6 +131,47 @@ import * as cdk from '@aws-cdk/core'; } } ``` +There are a few templates for _Secrets Manager_ included in the library to make it more simpler +to handle standard credentials to e.g. PostgresSQL +```go +type MyServiceContext struct { + DbCtx support.SecretsManagerRDSPostgreSQLRotationSingleUser `asm:"dbctx, strkey=password"` + Settings struct { + BatchSize int `json:"batchsize"` + Signer string `json:"signer,omitempty"` + } `pms:"settings"` +} +``` +If this is reported it may output something like this +```json +{ + "type": "secrets-manager", + "fqname": "/prod/test-service/dbctx", + "keyid": "", + "description": "", + "tags": {}, + "details": { + "strkey": "password" + }, + "value": "{\"engine\":\"postgres\",\"host\":\"pgsql-17.toffia.se\",\"username\":\"gördis\",\"dbname\":\"mydb\"}", + "valuetype": "SecureString" +}, +{ + "type": "parameter-store", + "fqname": "/prod/test-service/settings", + "keyid": "", + "description": "", + "tags": {}, + "details": { + "pattern": "", + "tier": "Standard" + }, + "value": "{\"batchsize\":77,\"signer\":\"mto\"}", + "valuetype": "String" +} +``` + +This may then be used to generate CDK artifacts (as above) using [ssm-cdk-generator](https://www.npmjs.com/package/ssm-cdk-generator) to generate passwords and the secrets using Cloud Formation deployment. # Standard Usage diff --git a/internal/testsupport/teststructs.go b/internal/testsupport/teststructs.go index 6ffcf25..a76f57e 100644 --- a/internal/testsupport/teststructs.go +++ b/internal/testsupport/teststructs.go @@ -1,5 +1,7 @@ package testsupport +import "github.com/mariotoffia/ssm.git/support" + // SingleStringPmsStruct with single string type SingleStringPmsStruct struct { Name string `pms:"test, prefix=simple,tag1=nanna banna panna"` @@ -58,3 +60,12 @@ type MyDbServiceConfigAsm struct { Timeout int `json:"timeout"` } `asm:"bubbibobbo, strkey=password"` } + +// MyContextPostgresSQL demo context +type MyContextPostgresSQL struct { + DbCtx support.SecretsManagerRDSPostgreSQLRotationSingleUser `asm:"dbctx, strkey=password"` + Settings struct { + BatchSize int `json:"batchsize"` + Signer string `json:"signer,omitempty"` + } `pms:"settings"` +} diff --git a/report/report_test.go b/report/report_test.go index 8ec19a3..1516980 100644 --- a/report/report_test.go +++ b/report/report_test.go @@ -7,6 +7,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/ssm" "github.com/mariotoffia/ssm.git/internal/asm" + "github.com/mariotoffia/ssm.git/internal/pms" "github.com/mariotoffia/ssm.git/internal/testsupport" "github.com/mariotoffia/ssm.git/parser" "github.com/mariotoffia/ssm.git/support" @@ -117,3 +118,33 @@ func TestReportSingleAsmSecretSubJsonNilStruct(t *testing.T) { assert.Contains(t, buff, `"fqname": "/prod/test-service/bubbibobbo"`) fmt.Println(buff) } + +func TestReportPostgresSQLTemplateStruct(t *testing.T) { + test := testsupport.MyContextPostgresSQL{} + test.DbCtx.DbName = "mydb" + test.DbCtx.Engine = support.PostgresDBEngine + test.DbCtx.Host = "pgsql-17.toffia.se" + test.DbCtx.Username = "gördis" + test.Settings.BatchSize = 77 + test.Settings.Signer = "mto" + + tp := reflect.ValueOf(&test) + + node, err := parser.New("test-service", "prod", ""). + RegisterTagParser("asm", asm.NewTagParser()). + RegisterTagParser("pms", pms.NewTagParser()). + Parse(tp) + + if err != nil { + assert.Equal(t, nil, err) + } + + reporter := NewWithTier(ssm.ParameterTierStandard) + report, buff, err := reporter.RenderReport(node, &support.FieldFilters{}, true) + if err != nil { + assert.Equal(t, nil, err) + } + + assert.Equal(t, 2, len(report.Parameters)) + fmt.Println(buff) +}