@@ -69,18 +69,11 @@ func NewSecretManagerSigner(ctx context.Context, publicKeySecretName, privateKey
69
69
defer client .Close ()
70
70
71
71
// Public Key
72
- publicKeyRaw , err := accessSecretVersion (ctx , client , publicKeySecretName )
72
+ var publicKey crypto.PublicKey
73
+ pemBlock , err := secretPEM (ctx , client , publicKeySecretName )
73
74
if err != nil {
74
- return nil , fmt .Errorf ("failed to access public key secret (%s): %w" , publicKeySecretName , err )
75
- }
76
- pemBlock , rest := pem .Decode ([]byte (publicKeyRaw ))
77
- if pemBlock == nil {
78
- return nil , errors .New ("failed to decode PEM" )
79
- }
80
- if len (rest ) > 0 {
81
- return nil , fmt .Errorf ("extra data after decoding PEM: %v" , rest )
75
+ return nil , fmt .Errorf ("failed to get public key secret PEM (%s): %w" , publicKeySecretName , err )
82
76
}
83
- var publicKey crypto.PublicKey
84
77
switch pemBlock .Type {
85
78
case "PUBLIC KEY" :
86
79
publicKey , err = x509 .ParsePKIXPublicKey (pemBlock .Bytes )
@@ -92,18 +85,11 @@ func NewSecretManagerSigner(ctx context.Context, publicKeySecretName, privateKey
92
85
}
93
86
94
87
// Private Key
95
- privateKeyRaw , err := accessSecretVersion (ctx , client , privateKeySecretName )
88
+ var privateKey crypto.PrivateKey
89
+ pemBlock , err = secretPEM (ctx , client , privateKeySecretName )
96
90
if err != nil {
97
- return nil , fmt .Errorf ("failed to access private key secret (%s): %w" , privateKeySecretName , err )
98
- }
99
- pemBlock , rest = pem .Decode ([]byte (privateKeyRaw ))
100
- if pemBlock == nil {
101
- return nil , errors .New ("failed to decode PEM" )
91
+ return nil , fmt .Errorf ("failed to get private key secret PEM (%s): %w" , privateKeySecretName , err )
102
92
}
103
- if len (rest ) > 0 {
104
- return nil , fmt .Errorf ("extra data after decoding PEM: %v" , rest )
105
- }
106
- var privateKey crypto.PrivateKey
107
93
switch pemBlock .Type {
108
94
case "EC PRIVATE KEY" :
109
95
privateKey , err = x509 .ParseECPrivateKey (pemBlock .Bytes )
@@ -120,7 +106,7 @@ func NewSecretManagerSigner(ctx context.Context, publicKeySecretName, privateKey
120
106
}, nil
121
107
}
122
108
123
- func accessSecretVersion (ctx context.Context , client * secretmanager.Client , secretName string ) ([] byte , error ) {
109
+ func secretPEM (ctx context.Context , client * secretmanager.Client , secretName string ) (* pem. Block , error ) {
124
110
resp , err := client .AccessSecretVersion (ctx , & secretmanagerpb.AccessSecretVersionRequest {
125
111
Name : secretName ,
126
112
})
@@ -137,5 +123,13 @@ func accessSecretVersion(ctx context.Context, client *secretmanager.Client, secr
137
123
return nil , errors .New ("Data corruption detected." )
138
124
}
139
125
140
- return resp .Payload .Data , nil
126
+ pemBlock , rest := pem .Decode ([]byte (resp .Payload .Data ))
127
+ if pemBlock == nil {
128
+ return nil , errors .New ("failed to decode PEM" )
129
+ }
130
+ if len (rest ) > 0 {
131
+ return nil , fmt .Errorf ("extra data after decoding PEM: %v" , rest )
132
+ }
133
+
134
+ return pemBlock , nil
141
135
}
0 commit comments