1
1
import { SecretsManagerClient , GetSecretValueCommand } from '@aws-sdk/client-secrets-manager' ;
2
2
3
- import type { Serverless , ServerlessSecretHooks , ServerlessSecretOptions } from './index.types' ;
3
+ import type { Serverless , ServerlessSecretHooks , ServerlessSecretOptions , ServerlessOptions } from './index.types' ;
4
4
5
5
class ServerlessAWSSecret {
6
+ Error : ErrorConstructor ;
6
7
hooks : ServerlessSecretHooks ;
8
+ log : NonNullable < ServerlessOptions [ 'log' ] > ;
7
9
options : ServerlessSecretOptions ;
8
10
serverless : Serverless ;
9
11
10
- constructor ( serverless : Serverless ) {
12
+ constructor ( serverless : Serverless , cliOptions : unknown , options : ServerlessOptions ) {
11
13
this . setOptions ( serverless ) ;
14
+ this . setLogger ( options ) ;
12
15
13
16
this . serverless = serverless ;
17
+ this . Error = serverless . classes ?. Error ?? Error ;
14
18
15
19
this . hooks = {
16
20
'before:package:initialize' : this . loadSecrets . bind ( this ) ,
@@ -25,7 +29,7 @@ class ServerlessAWSSecret {
25
29
const { SecretString } = await client . send ( command ) ;
26
30
27
31
if ( ! SecretString ) {
28
- throw new this . serverless . classes . Error ( `Failed to retrieve the secret: ${ this . options . secretId } ` ) ;
32
+ throw new this . Error ( `Failed to retrieve the secret: ${ this . options . secretId } ` ) ;
29
33
}
30
34
31
35
const secrets = JSON . parse ( SecretString ) ;
@@ -36,20 +40,18 @@ class ServerlessAWSSecret {
36
40
const secretKey = value . replace ( this . options . secretPrefix ! , '' ) ;
37
41
38
42
if ( ! secrets [ secretKey ] ) {
39
- throw new Error ( `Secret ${ secretKey } do not exist` ) ;
43
+ throw new this . Error ( `Secret ${ secretKey } do not exist` ) ;
40
44
}
41
45
42
- if ( this . options . verbose ) {
43
- console . log ( `[serverless-aws-secrets]: Replacing ${ key } with secret of ${ secretKey } ` ) ;
44
- }
46
+ this . log . verbose ( `[serverless-aws-secrets]: Replacing ${ key } with secret of ${ secretKey } ` ) ;
45
47
46
48
this . serverless . service . provider . environment [ key ] = secrets [ secretKey ] ;
47
49
48
50
++ replaceCount ;
49
51
}
50
52
}
51
53
52
- console . log ( `[serverless-aws-secrets]: Replaced ${ replaceCount } secrets in environment variables` ) ;
54
+ this . log . success ( `[serverless-aws-secrets]: Replaced ${ replaceCount } secrets in environment variables` ) ;
53
55
}
54
56
55
57
setOptions ( serverless : Serverless ) {
@@ -60,6 +62,13 @@ class ServerlessAWSSecret {
60
62
this . options . verbose = this . options . verbose ?? false ;
61
63
}
62
64
65
+ setLogger ( options : ServerlessOptions ) {
66
+ this . log = {
67
+ verbose : options . log ?. verbose ?? this . options . verbose ? console . log : ( ) => { } ,
68
+ success : options . log ?. success ?? console . log ,
69
+ } ;
70
+ }
71
+
63
72
getSecretId ( serverless : Serverless ) {
64
73
if ( this . options ?. secretId ) {
65
74
return this . options . secretId ;
0 commit comments