Skip to content

Commit 88aaf3f

Browse files
fix: naming, typos and error handling (#50)
* fix: naming, typos and error handling * fixing default values and adding more tests
1 parent 85a313d commit 88aaf3f

4 files changed

Lines changed: 102 additions & 22 deletions

File tree

xtypes/ecdsa_pub.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,22 @@ func (d *ECDSAPubKey) ValueValid(s string) error {
7373
// GetDefaultValue will be used to read the default value when showing usage
7474
// information.
7575
func (d *ECDSAPubKey) GetDefaultValue() (string, error) {
76-
// TODO show the public key
77-
return "<secret>", nil
76+
if d.DefaultValue == nil {
77+
return "", nil
78+
}
79+
der, err := x509.MarshalPKIXPublicKey(d.DefaultValue)
80+
if err != nil {
81+
return "", err
82+
}
83+
pemBlock := &pem.Block{
84+
Type: "PUBLIC KEY",
85+
Bytes: der,
86+
}
87+
pemBytes := pem.EncodeToMemory(pemBlock)
88+
if d.Base64Encoder != nil {
89+
return d.Base64Encoder.EncodeToString(pemBytes), nil
90+
}
91+
return string(pemBytes), nil
7892
}
7993

8094
func parseECPubKey(v string, base64Enc *base64.Encoding) (*ecdsa.PublicKey, error) {
@@ -104,12 +118,12 @@ func parseECPubKey(v string, base64Enc *base64.Encoding) (*ecdsa.PublicKey, erro
104118
var err error
105119
pubK, err = x509.ParsePKIXPublicKey(pemBlock.Bytes)
106120
if err != nil {
107-
return nil, fmt.Errorf("error decoding PEM block as ANS.1 public key: %w", err)
121+
return nil, fmt.Errorf("error decoding PEM block as ASN.1 public key: %w", err)
108122
}
109123

110124
ecpubK, ok := pubK.(*ecdsa.PublicKey)
111125
if !ok {
112-
return nil, fmt.Errorf("expected key of type *ecdsa.pubateKey, but got type: %T", pubK)
126+
return nil, fmt.Errorf("expected key of type *ecdsa.PublicKey, but got type: %T", pubK)
113127
}
114128

115129
return ecpubK, nil

xtypes/ecdsa_pub_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/elliptic"
66
"crypto/rand"
77
"crypto/x509"
8+
"encoding/base64"
89
"encoding/pem"
910
"reflect"
1011
"testing"
@@ -140,3 +141,31 @@ func generateTestECPubKey(t *testing.T) (*ecdsa.PublicKey, string) {
140141
}
141142
return &privateKey.PublicKey, string(pem.EncodeToMemory(pemBlock))
142143
}
144+
145+
func TestECDSAPubKey_GetDefaultValue(t *testing.T) {
146+
pub, pubPEM := generateTestECPubKey(t)
147+
148+
t.Run("nil default", func(t *testing.T) {
149+
xt := &xtypes.ECDSAPubKey{DefaultValue: nil}
150+
val, err := xt.GetDefaultValue()
151+
assert.NoError(t, err)
152+
assert.Equal(t, "", val)
153+
})
154+
155+
t.Run("standard encoding", func(t *testing.T) {
156+
xt := &xtypes.ECDSAPubKey{DefaultValue: pub}
157+
val, err := xt.GetDefaultValue()
158+
assert.NoError(t, err)
159+
assert.Equal(t, pubPEM, val)
160+
})
161+
162+
t.Run("with base64 encoder", func(t *testing.T) {
163+
xt := &xtypes.ECDSAPubKey{
164+
DefaultValue: pub,
165+
Base64Encoder: base64.StdEncoding,
166+
}
167+
val, err := xt.GetDefaultValue()
168+
assert.NoError(t, err)
169+
assert.Equal(t, base64.StdEncoding.EncodeToString([]byte(pubPEM)), val)
170+
})
171+
}

xtypes/x25519_pub.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
"github.com/simplesurance/proteus/types"
1313
)
1414

15-
// X25519PublicKey is a xtype for *ecdh.PublicKey. The key format is expected
15+
// X25519PubKey is a xtype for *ecdh.PublicKey. The key format is expected
1616
// to be on PKIX/PEM format, hex encoded (32 bytes), or raw bytes (optionally
1717
// base64 encoded).
18-
type X25519PublicKey struct {
18+
type X25519PubKey struct {
1919
DefaultValue *ecdh.PublicKey
2020
UpdateFn func(*ecdh.PublicKey)
2121
Base64Encoder *base64.Encoding
@@ -25,10 +25,10 @@ type X25519PublicKey struct {
2525
}
2626
}
2727

28-
var _ types.XType = &X25519PublicKey{}
28+
var _ types.XType = &X25519PubKey{}
2929

3030
// UnmarshalParam parses the input as a string.
31-
func (d *X25519PublicKey) UnmarshalParam(in *string) error {
31+
func (d *X25519PubKey) UnmarshalParam(in *string) error {
3232
var pubK *ecdh.PublicKey
3333
if in != nil && *in != "" {
3434
var err error
@@ -52,7 +52,7 @@ func (d *X25519PublicKey) UnmarshalParam(in *string) error {
5252
// Value reads the current updated value, taking the default value into
5353
// consideration. If the parameter is not marked as optional, this is
5454
// guaranteed to be not nil.
55-
func (d *X25519PublicKey) Value() *ecdh.PublicKey {
55+
func (d *X25519PubKey) Value() *ecdh.PublicKey {
5656
d.content.mutex.Lock()
5757
defer d.content.mutex.Unlock()
5858

@@ -65,7 +65,7 @@ func (d *X25519PublicKey) Value() *ecdh.PublicKey {
6565

6666
// ValueValid test if the provided parameter value is valid. Has no side
6767
// effects.
68-
func (d *X25519PublicKey) ValueValid(s string) error {
68+
func (d *X25519PubKey) ValueValid(s string) error {
6969
if s == "" {
7070
return types.ErrNoValue
7171
}
@@ -75,9 +75,14 @@ func (d *X25519PublicKey) ValueValid(s string) error {
7575

7676
// GetDefaultValue will be used to read the default value when showing usage
7777
// information.
78-
func (d *X25519PublicKey) GetDefaultValue() (string, error) {
79-
// TODO show the public key
80-
return "<secret>", nil
78+
func (d *X25519PubKey) GetDefaultValue() (string, error) {
79+
if d.DefaultValue == nil {
80+
return "", nil
81+
}
82+
if d.Base64Encoder != nil {
83+
return d.Base64Encoder.EncodeToString(d.DefaultValue.Bytes()), nil
84+
}
85+
return hex.EncodeToString(d.DefaultValue.Bytes()), nil
8186
}
8287

8388
func parseX25519PublicKey(v string, base64Enc *base64.Encoding) (*ecdh.PublicKey, error) {
@@ -103,7 +108,7 @@ func parseX25519PublicKey(v string, base64Enc *base64.Encoding) (*ecdh.PublicKey
103108
}
104109
pubK, err := x509.ParsePKIXPublicKey(pemBlock.Bytes)
105110
if err != nil {
106-
return nil, fmt.Errorf("error decoding PEM block as ANS.1 public key: %w", err)
111+
return nil, fmt.Errorf("error decoding PEM block as ASN.1 public key: %w", err)
107112
}
108113
xPubK, ok := pubK.(*ecdh.PublicKey)
109114
if !ok || xPubK.Curve() != ecdh.X25519() {

xtypes/x25519_test.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestX25519Keys(t *testing.T) {
2222

2323
cfg := struct {
2424
Priv *xtypes.X25519PrivateKey
25-
Pub *xtypes.X25519PublicKey
25+
Pub *xtypes.X25519PubKey
2626
}{}
2727

2828
testProvider := cfgtest.New(types.ParamValues{
@@ -49,7 +49,7 @@ func TestX25519KeysHex(t *testing.T) {
4949

5050
cfg := struct {
5151
Priv *xtypes.X25519PrivateKey
52-
Pub *xtypes.X25519PublicKey
52+
Pub *xtypes.X25519PubKey
5353
}{}
5454

5555
testProvider := cfgtest.New(types.ParamValues{
@@ -76,10 +76,10 @@ func TestX25519KeysBase64Raw(t *testing.T) {
7676

7777
cfg := struct {
7878
Priv *xtypes.X25519PrivateKey
79-
Pub *xtypes.X25519PublicKey
79+
Pub *xtypes.X25519PubKey
8080
}{
8181
Priv: &xtypes.X25519PrivateKey{Base64Encoder: base64.StdEncoding},
82-
Pub: &xtypes.X25519PublicKey{Base64Encoder: base64.StdEncoding},
82+
Pub: &xtypes.X25519PubKey{Base64Encoder: base64.StdEncoding},
8383
}
8484

8585
testProvider := cfgtest.New(types.ParamValues{
@@ -106,10 +106,10 @@ func TestX25519KeysBase64PEM(t *testing.T) {
106106

107107
cfg := struct {
108108
Priv *xtypes.X25519PrivateKey
109-
Pub *xtypes.X25519PublicKey
109+
Pub *xtypes.X25519PubKey
110110
}{
111111
Priv: &xtypes.X25519PrivateKey{Base64Encoder: base64.StdEncoding},
112-
Pub: &xtypes.X25519PublicKey{Base64Encoder: base64.StdEncoding},
112+
Pub: &xtypes.X25519PubKey{Base64Encoder: base64.StdEncoding},
113113
}
114114

115115
testProvider := cfgtest.New(types.ParamValues{
@@ -137,7 +137,7 @@ func TestX25519ValueValid(t *testing.T) {
137137
assert.Error(t, priv.ValueValid(""))
138138

139139
_, pubPEM := generateTestX25519PubKey(t, nil)
140-
pub := &xtypes.X25519PublicKey{}
140+
pub := &xtypes.X25519PubKey{}
141141

142142
assert.NoError(t, pub.ValueValid(pubPEM))
143143
assert.NoError(t, pub.ValueValid(hex.EncodeToString(make([]byte, 32))))
@@ -168,7 +168,10 @@ func generateTestX25519PubKey(t *testing.T, priv *ecdh.PrivateKey) (*ecdh.Public
168168
if priv != nil {
169169
pub = priv.PublicKey()
170170
} else {
171-
newPriv, _ := ecdh.X25519().GenerateKey(rand.Reader)
171+
newPriv, err := ecdh.X25519().GenerateKey(rand.Reader)
172+
if err != nil {
173+
t.Fatalf("failed to generate X25519 private key: %v", err)
174+
}
172175
pub = newPriv.PublicKey()
173176
}
174177

@@ -182,3 +185,32 @@ func generateTestX25519PubKey(t *testing.T, priv *ecdh.PrivateKey) (*ecdh.Public
182185
}
183186
return pub, string(pem.EncodeToMemory(pemBlock))
184187
}
188+
189+
func TestX25519PubKey_GetDefaultValue(t *testing.T) {
190+
pub, _ := generateTestX25519PubKey(t, nil)
191+
pubHex := hex.EncodeToString(pub.Bytes())
192+
193+
t.Run("nil default", func(t *testing.T) {
194+
xt := &xtypes.X25519PubKey{DefaultValue: nil}
195+
val, err := xt.GetDefaultValue()
196+
assert.NoError(t, err)
197+
assert.Equal(t, "", val)
198+
})
199+
200+
t.Run("standard encoding (hex)", func(t *testing.T) {
201+
xt := &xtypes.X25519PubKey{DefaultValue: pub}
202+
val, err := xt.GetDefaultValue()
203+
assert.NoError(t, err)
204+
assert.Equal(t, pubHex, val)
205+
})
206+
207+
t.Run("with base64 encoder", func(t *testing.T) {
208+
xt := &xtypes.X25519PubKey{
209+
DefaultValue: pub,
210+
Base64Encoder: base64.StdEncoding,
211+
}
212+
val, err := xt.GetDefaultValue()
213+
assert.NoError(t, err)
214+
assert.Equal(t, base64.StdEncoding.EncodeToString(pub.Bytes()), val)
215+
})
216+
}

0 commit comments

Comments
 (0)