Skip to content

Commit d869544

Browse files
code-lucidal58vvbjirikuncarsebastien-rosset
authored
[Go][Client] Secret key content string in http signing support (#8570)
* accept private key content string * sample update * Add comments to new methods * update samples with comments * Update modules/openapi-generator/src/main/resources/go/signing.mustache Co-authored-by: Jiri Kuncar <jiri.kuncar@gmail.com> * Update modules/openapi-generator/src/main/resources/go/signing.mustache Co-authored-by: Jiri Kuncar <jiri.kuncar@gmail.com> * Update signing.mustache * update sample comments * Update modules/openapi-generator/src/main/resources/go/signing.mustache Co-authored-by: Sebastien Rosset <serosset@cisco.com> * Update modules/openapi-generator/src/main/resources/go/signing.mustache Co-authored-by: Sebastien Rosset <serosset@cisco.com> * update empty checks for privateKey Co-authored-by: Vikrant Balyan <vvb@users.noreply.github.com> Co-authored-by: Jiri Kuncar <jiri.kuncar@gmail.com> Co-authored-by: Sebastien Rosset <serosset@cisco.com>
1 parent d7bdd7f commit d869544

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

modules/openapi-generator/src/main/resources/go/signing.mustache

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,19 @@ type HttpSignatureAuth struct {
116116
privateKey crypto.PrivateKey // The private key used to sign HTTP requests.
117117
}
118118

119+
// SetPrivateKey accepts a private key string and sets it.
120+
func (h *HttpSignatureAuth) SetPrivateKey(privateKey string) error {
121+
return h.parsePrivateKey([]byte(privateKey))
122+
}
123+
119124
// ContextWithValue validates the HttpSignatureAuth configuration parameters and returns a context
120125
// suitable for HTTP signature. An error is returned if the HttpSignatureAuth configuration parameters
121126
// are invalid.
122127
func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Context, error) {
123128
if h.KeyId == "" {
124129
return nil, fmt.Errorf("Key ID must be specified")
125130
}
126-
if h.PrivateKeyPath == "" {
131+
if h.PrivateKeyPath == "" && h.privateKey == nil {
127132
return nil, fmt.Errorf("Private key path must be specified")
128133
}
129134
if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok {
@@ -168,7 +173,11 @@ func (h *HttpSignatureAuth) GetPublicKey() (crypto.PublicKey, error) {
168173
}
169174

170175
// loadPrivateKey reads the private key from the file specified in the HttpSignatureAuth.
176+
// The key is loaded only when privateKey is not already set.
171177
func (h *HttpSignatureAuth) loadPrivateKey() (err error) {
178+
if h.privateKey != nil {
179+
return nil
180+
}
172181
var file *os.File
173182
file, err = os.Open(h.PrivateKeyPath)
174183
if err != nil {
@@ -182,12 +191,18 @@ func (h *HttpSignatureAuth) loadPrivateKey() (err error) {
182191
if err != nil {
183192
return err
184193
}
194+
return h.parsePrivateKey(priv)
195+
}
196+
197+
// parsePrivateKey decodes privateKey byte array to crypto.PrivateKey type.
198+
func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error {
185199
pemBlock, _ := pem.Decode(priv)
186200
if pemBlock == nil {
187201
// No PEM data has been found.
188202
return fmt.Errorf("File '%s' does not contain PEM data", h.PrivateKeyPath)
189203
}
190204
var privKey []byte
205+
var err error
191206
if x509.IsEncryptedPEMBlock(pemBlock) {
192207
// The PEM data is encrypted.
193208
privKey, err = x509.DecryptPEMBlock(pemBlock, []byte(h.Passphrase))

samples/openapi3/client/petstore/go/go-petstore/signing.go

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)