From 8ae175b1f706c8ddcaf3575f2d138672b1edcfa0 Mon Sep 17 00:00:00 2001 From: code-lucidal58 <aanisha.mishra05@gmail.com> Date: Thu, 28 Jan 2021 23:43:31 +0530 Subject: [PATCH 01/11] accept private key content string --- .../src/main/resources/go/signing.mustache | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/go/signing.mustache b/modules/openapi-generator/src/main/resources/go/signing.mustache index db486a16b20..e23d19972ef 100644 --- a/modules/openapi-generator/src/main/resources/go/signing.mustache +++ b/modules/openapi-generator/src/main/resources/go/signing.mustache @@ -116,6 +116,10 @@ type HttpSignatureAuth struct { privateKey crypto.PrivateKey // The private key used to sign HTTP requests. } +func (h *HttpSignatureAuth) SetPrivateKey(privateKey string) error { + return h.parsePrivateKey([]byte(privateKey)) +} + // ContextWithValue validates the HttpSignatureAuth configuration parameters and returns a context // suitable for HTTP signature. An error is returned if the HttpSignatureAuth configuration parameters // are invalid. @@ -123,7 +127,7 @@ func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Conte if h.KeyId == "" { return nil, fmt.Errorf("Key ID must be specified") } - if h.PrivateKeyPath == "" { + if h.PrivateKeyPath == "" && h.privateKey == "" { return nil, fmt.Errorf("Private key path must be specified") } if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok { @@ -169,6 +173,9 @@ func (h *HttpSignatureAuth) GetPublicKey() (crypto.PublicKey, error) { // loadPrivateKey reads the private key from the file specified in the HttpSignatureAuth. func (h *HttpSignatureAuth) loadPrivateKey() (err error) { + if h.privateKey != nil { + return nil + } var file *os.File file, err = os.Open(h.PrivateKeyPath) if err != nil { @@ -182,12 +189,17 @@ func (h *HttpSignatureAuth) loadPrivateKey() (err error) { if err != nil { return err } + return h.parsePrivateKey(priv) +} + +func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error { pemBlock, _ := pem.Decode(priv) if pemBlock == nil { // No PEM data has been found. return fmt.Errorf("File '%s' does not contain PEM data", h.PrivateKeyPath) } var privKey []byte + var err error if x509.IsEncryptedPEMBlock(pemBlock) { // The PEM data is encrypted. privKey, err = x509.DecryptPEMBlock(pemBlock, []byte(h.Passphrase)) -- GitLab From 0a1d5e6ea587141ec8b8c96afd20cef9746589d6 Mon Sep 17 00:00:00 2001 From: code-lucidal58 <aanisha.mishra05@gmail.com> Date: Fri, 29 Jan 2021 00:04:41 +0530 Subject: [PATCH 02/11] sample update --- .../client/petstore/go/go-petstore/signing.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/samples/openapi3/client/petstore/go/go-petstore/signing.go b/samples/openapi3/client/petstore/go/go-petstore/signing.go index f4be5ad9265..468d58348fe 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/signing.go +++ b/samples/openapi3/client/petstore/go/go-petstore/signing.go @@ -125,6 +125,10 @@ type HttpSignatureAuth struct { privateKey crypto.PrivateKey // The private key used to sign HTTP requests. } +func (h *HttpSignatureAuth) SetPrivateKey(privateKey string) error { + return h.parsePrivateKey([]byte(privateKey)) +} + // ContextWithValue validates the HttpSignatureAuth configuration parameters and returns a context // suitable for HTTP signature. An error is returned if the HttpSignatureAuth configuration parameters // are invalid. @@ -132,7 +136,7 @@ func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Conte if h.KeyId == "" { return nil, fmt.Errorf("Key ID must be specified") } - if h.PrivateKeyPath == "" { + if h.PrivateKeyPath == "" && h.privateKey == "" { return nil, fmt.Errorf("Private key path must be specified") } if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok { @@ -178,6 +182,9 @@ func (h *HttpSignatureAuth) GetPublicKey() (crypto.PublicKey, error) { // loadPrivateKey reads the private key from the file specified in the HttpSignatureAuth. func (h *HttpSignatureAuth) loadPrivateKey() (err error) { + if h.privateKey != nil { + return nil + } var file *os.File file, err = os.Open(h.PrivateKeyPath) if err != nil { @@ -191,12 +198,17 @@ func (h *HttpSignatureAuth) loadPrivateKey() (err error) { if err != nil { return err } + return h.parsePrivateKey(priv) +} + +func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error { pemBlock, _ := pem.Decode(priv) if pemBlock == nil { // No PEM data has been found. return fmt.Errorf("File '%s' does not contain PEM data", h.PrivateKeyPath) } var privKey []byte + var err error if x509.IsEncryptedPEMBlock(pemBlock) { // The PEM data is encrypted. privKey, err = x509.DecryptPEMBlock(pemBlock, []byte(h.Passphrase)) -- GitLab From 6b25d254851d934280c31dfb6cdbc8c6057c4497 Mon Sep 17 00:00:00 2001 From: Aanisha Mishra <aanisha.mishra05@gmail.com> Date: Fri, 29 Jan 2021 11:30:45 +0530 Subject: [PATCH 03/11] Add comments to new methods --- .../openapi-generator/src/main/resources/go/signing.mustache | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/go/signing.mustache b/modules/openapi-generator/src/main/resources/go/signing.mustache index e23d19972ef..ae3419f9872 100644 --- a/modules/openapi-generator/src/main/resources/go/signing.mustache +++ b/modules/openapi-generator/src/main/resources/go/signing.mustache @@ -116,6 +116,7 @@ type HttpSignatureAuth struct { privateKey crypto.PrivateKey // The private key used to sign HTTP requests. } +//SetPrivateKey - accepts a private key string and sets it func (h *HttpSignatureAuth) SetPrivateKey(privateKey string) error { return h.parsePrivateKey([]byte(privateKey)) } @@ -192,6 +193,7 @@ func (h *HttpSignatureAuth) loadPrivateKey() (err error) { return h.parsePrivateKey(priv) } +// parsePrivateKey - decode privateKey byte array to crypto.PrivateKey type func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error { pemBlock, _ := pem.Decode(priv) if pemBlock == nil { -- GitLab From 37c668ae6a059ccc16422f2cde4f194ee913f7c1 Mon Sep 17 00:00:00 2001 From: code-lucidal58 <aanisha.mishra05@gmail.com> Date: Fri, 29 Jan 2021 12:16:04 +0530 Subject: [PATCH 04/11] update samples with comments --- samples/openapi3/client/petstore/go/go-petstore/signing.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/openapi3/client/petstore/go/go-petstore/signing.go b/samples/openapi3/client/petstore/go/go-petstore/signing.go index 468d58348fe..63b89d413fa 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/signing.go +++ b/samples/openapi3/client/petstore/go/go-petstore/signing.go @@ -125,6 +125,7 @@ type HttpSignatureAuth struct { privateKey crypto.PrivateKey // The private key used to sign HTTP requests. } +//SetPrivateKey - accepts a private key string and sets it func (h *HttpSignatureAuth) SetPrivateKey(privateKey string) error { return h.parsePrivateKey([]byte(privateKey)) } @@ -201,6 +202,7 @@ func (h *HttpSignatureAuth) loadPrivateKey() (err error) { return h.parsePrivateKey(priv) } +// parsePrivateKey - decode privateKey byte array to crypto.PrivateKey type func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error { pemBlock, _ := pem.Decode(priv) if pemBlock == nil { -- GitLab From ef5e2a41f86da27739f568e5faa8ef6840f5247c Mon Sep 17 00:00:00 2001 From: Vikrant Balyan <vvb@users.noreply.github.com> Date: Fri, 29 Jan 2021 17:21:04 +0530 Subject: [PATCH 05/11] Update modules/openapi-generator/src/main/resources/go/signing.mustache Co-authored-by: Jiri Kuncar <jiri.kuncar@gmail.com> --- .../openapi-generator/src/main/resources/go/signing.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/go/signing.mustache b/modules/openapi-generator/src/main/resources/go/signing.mustache index ae3419f9872..5c8cff2716a 100644 --- a/modules/openapi-generator/src/main/resources/go/signing.mustache +++ b/modules/openapi-generator/src/main/resources/go/signing.mustache @@ -116,7 +116,7 @@ type HttpSignatureAuth struct { privateKey crypto.PrivateKey // The private key used to sign HTTP requests. } -//SetPrivateKey - accepts a private key string and sets it +//SetPrivateKey accepts a private key string and sets it. func (h *HttpSignatureAuth) SetPrivateKey(privateKey string) error { return h.parsePrivateKey([]byte(privateKey)) } -- GitLab From b7858e529b78bc461449c3ac6f618742e1dbb029 Mon Sep 17 00:00:00 2001 From: Vikrant Balyan <vvb@users.noreply.github.com> Date: Fri, 29 Jan 2021 17:21:13 +0530 Subject: [PATCH 06/11] Update modules/openapi-generator/src/main/resources/go/signing.mustache Co-authored-by: Jiri Kuncar <jiri.kuncar@gmail.com> --- .../openapi-generator/src/main/resources/go/signing.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/go/signing.mustache b/modules/openapi-generator/src/main/resources/go/signing.mustache index 5c8cff2716a..208ce4add1e 100644 --- a/modules/openapi-generator/src/main/resources/go/signing.mustache +++ b/modules/openapi-generator/src/main/resources/go/signing.mustache @@ -193,7 +193,7 @@ func (h *HttpSignatureAuth) loadPrivateKey() (err error) { return h.parsePrivateKey(priv) } -// parsePrivateKey - decode privateKey byte array to crypto.PrivateKey type +// parsePrivateKey decodes privateKey byte array to crypto.PrivateKey type. func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error { pemBlock, _ := pem.Decode(priv) if pemBlock == nil { -- GitLab From 5d869b1306ba9d87bb4dcc4194da796c33e5949d Mon Sep 17 00:00:00 2001 From: Vikrant Balyan <vvb@users.noreply.github.com> Date: Fri, 29 Jan 2021 17:27:01 +0530 Subject: [PATCH 07/11] Update signing.mustache --- modules/openapi-generator/src/main/resources/go/signing.mustache | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/openapi-generator/src/main/resources/go/signing.mustache b/modules/openapi-generator/src/main/resources/go/signing.mustache index 208ce4add1e..b2aeb00405e 100644 --- a/modules/openapi-generator/src/main/resources/go/signing.mustache +++ b/modules/openapi-generator/src/main/resources/go/signing.mustache @@ -173,6 +173,7 @@ func (h *HttpSignatureAuth) GetPublicKey() (crypto.PublicKey, error) { } // loadPrivateKey reads the private key from the file specified in the HttpSignatureAuth. +// They key is loaded only when privateKey is not already set. func (h *HttpSignatureAuth) loadPrivateKey() (err error) { if h.privateKey != nil { return nil -- GitLab From 770f3b6144f7c0f33454904bb9fc5cbc4e9c2643 Mon Sep 17 00:00:00 2001 From: code-lucidal58 <aanisha.mishra05@gmail.com> Date: Fri, 29 Jan 2021 17:51:48 +0530 Subject: [PATCH 08/11] update sample comments --- samples/openapi3/client/petstore/go/go-petstore/signing.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/openapi3/client/petstore/go/go-petstore/signing.go b/samples/openapi3/client/petstore/go/go-petstore/signing.go index 63b89d413fa..a04b04c4120 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/signing.go +++ b/samples/openapi3/client/petstore/go/go-petstore/signing.go @@ -125,7 +125,7 @@ type HttpSignatureAuth struct { privateKey crypto.PrivateKey // The private key used to sign HTTP requests. } -//SetPrivateKey - accepts a private key string and sets it +//SetPrivateKey accepts a private key string and sets it. func (h *HttpSignatureAuth) SetPrivateKey(privateKey string) error { return h.parsePrivateKey([]byte(privateKey)) } @@ -182,6 +182,7 @@ func (h *HttpSignatureAuth) GetPublicKey() (crypto.PublicKey, error) { } // loadPrivateKey reads the private key from the file specified in the HttpSignatureAuth. +// They key is loaded only when privateKey is not already set. func (h *HttpSignatureAuth) loadPrivateKey() (err error) { if h.privateKey != nil { return nil @@ -202,7 +203,7 @@ func (h *HttpSignatureAuth) loadPrivateKey() (err error) { return h.parsePrivateKey(priv) } -// parsePrivateKey - decode privateKey byte array to crypto.PrivateKey type +// parsePrivateKey decodes privateKey byte array to crypto.PrivateKey type. func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error { pemBlock, _ := pem.Decode(priv) if pemBlock == nil { -- GitLab From c4d8b30893a7478e9a8fca3941d0ac48ed732bbe Mon Sep 17 00:00:00 2001 From: Vikrant Balyan <vvb@users.noreply.github.com> Date: Mon, 1 Feb 2021 14:03:01 +0530 Subject: [PATCH 09/11] Update modules/openapi-generator/src/main/resources/go/signing.mustache Co-authored-by: Sebastien Rosset <serosset@cisco.com> --- .../openapi-generator/src/main/resources/go/signing.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/go/signing.mustache b/modules/openapi-generator/src/main/resources/go/signing.mustache index b2aeb00405e..50e695d1324 100644 --- a/modules/openapi-generator/src/main/resources/go/signing.mustache +++ b/modules/openapi-generator/src/main/resources/go/signing.mustache @@ -173,7 +173,7 @@ func (h *HttpSignatureAuth) GetPublicKey() (crypto.PublicKey, error) { } // loadPrivateKey reads the private key from the file specified in the HttpSignatureAuth. -// They key is loaded only when privateKey is not already set. +// The key is loaded only when privateKey is not already set. func (h *HttpSignatureAuth) loadPrivateKey() (err error) { if h.privateKey != nil { return nil -- GitLab From 220f0d3d06e1359bb575e3e18168908996612ca4 Mon Sep 17 00:00:00 2001 From: Vikrant Balyan <vvb@users.noreply.github.com> Date: Mon, 1 Feb 2021 14:03:07 +0530 Subject: [PATCH 10/11] Update modules/openapi-generator/src/main/resources/go/signing.mustache Co-authored-by: Sebastien Rosset <serosset@cisco.com> --- .../openapi-generator/src/main/resources/go/signing.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/go/signing.mustache b/modules/openapi-generator/src/main/resources/go/signing.mustache index 50e695d1324..1f23c5bf74c 100644 --- a/modules/openapi-generator/src/main/resources/go/signing.mustache +++ b/modules/openapi-generator/src/main/resources/go/signing.mustache @@ -116,7 +116,7 @@ type HttpSignatureAuth struct { privateKey crypto.PrivateKey // The private key used to sign HTTP requests. } -//SetPrivateKey accepts a private key string and sets it. +// SetPrivateKey accepts a private key string and sets it. func (h *HttpSignatureAuth) SetPrivateKey(privateKey string) error { return h.parsePrivateKey([]byte(privateKey)) } -- GitLab From 540c1e54e44f1ebebdbcfa7d32360e1eb1e43131 Mon Sep 17 00:00:00 2001 From: code-lucidal58 <aanisha.mishra05@gmail.com> Date: Wed, 3 Feb 2021 09:07:59 +0530 Subject: [PATCH 11/11] update empty checks for privateKey --- .../src/main/resources/go/signing.mustache | 2 +- samples/openapi3/client/petstore/go/go-petstore/signing.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go/signing.mustache b/modules/openapi-generator/src/main/resources/go/signing.mustache index 1f23c5bf74c..22477a06f38 100644 --- a/modules/openapi-generator/src/main/resources/go/signing.mustache +++ b/modules/openapi-generator/src/main/resources/go/signing.mustache @@ -128,7 +128,7 @@ func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Conte if h.KeyId == "" { return nil, fmt.Errorf("Key ID must be specified") } - if h.PrivateKeyPath == "" && h.privateKey == "" { + if h.PrivateKeyPath == "" && h.privateKey == nil { return nil, fmt.Errorf("Private key path must be specified") } if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok { diff --git a/samples/openapi3/client/petstore/go/go-petstore/signing.go b/samples/openapi3/client/petstore/go/go-petstore/signing.go index a04b04c4120..9dc2cf7570a 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/signing.go +++ b/samples/openapi3/client/petstore/go/go-petstore/signing.go @@ -125,7 +125,7 @@ type HttpSignatureAuth struct { privateKey crypto.PrivateKey // The private key used to sign HTTP requests. } -//SetPrivateKey accepts a private key string and sets it. +// SetPrivateKey accepts a private key string and sets it. func (h *HttpSignatureAuth) SetPrivateKey(privateKey string) error { return h.parsePrivateKey([]byte(privateKey)) } @@ -137,7 +137,7 @@ func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Conte if h.KeyId == "" { return nil, fmt.Errorf("Key ID must be specified") } - if h.PrivateKeyPath == "" && h.privateKey == "" { + if h.PrivateKeyPath == "" && h.privateKey == nil { return nil, fmt.Errorf("Private key path must be specified") } if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok { @@ -182,7 +182,7 @@ func (h *HttpSignatureAuth) GetPublicKey() (crypto.PublicKey, error) { } // loadPrivateKey reads the private key from the file specified in the HttpSignatureAuth. -// They key is loaded only when privateKey is not already set. +// The key is loaded only when privateKey is not already set. func (h *HttpSignatureAuth) loadPrivateKey() (err error) { if h.privateKey != nil { return nil -- GitLab