Skip to content

Commit

Permalink
config: do base64 encoding smarter
Browse files Browse the repository at this point in the history
Rather than duplicating the structs that want to have transparent base64
encoding inside them, a newtype implementing encoding.TextMarshaler and
encoding.TextUnmarshaler does the trick without needing to import
yaml.v3 to satisfy the interface.

Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Nov 3, 2021
1 parent 752988a commit ae7e5a3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 59 deletions.
91 changes: 34 additions & 57 deletions config/auth.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
package config

import "encoding/base64"
import (
"encoding"
"encoding/base64"
)

// Base64 is a byte slice that encodes to and from base64-encoded strings.
type Base64 []byte

var (
_ encoding.TextMarshaler = (*Base64)(nil)
_ encoding.TextUnmarshaler = (*Base64)(nil)
)

// MarshalText implements encoding.TextMarshaler.
func (b *Base64) MarshalText() ([]byte, error) {
sz := base64.StdEncoding.EncodedLen(len(*b))
out := make([]byte, sz)
base64.StdEncoding.Encode(out, *b)
return out, nil
}

// UnmarshalText implements encoding.TextUnmarshaler.
func (b *Base64) UnmarshalText(in []byte) error {
sz := base64.StdEncoding.DecodedLen(len(in))
s := make([]byte, sz)
n, err := base64.StdEncoding.Decode(s, in)
if err != nil {
return err
}
*b = s[:n]
return nil
}

// Auth holds the specific configs for different authentication methods.
//
Expand All @@ -24,67 +55,13 @@ func (a Auth) Any() bool {
// "combo".
type AuthKeyserver struct {
API string `yaml:"api" json:"api"`
Intraservice []byte `yaml:"intraservice" json:"intraservice"`
}
type keyserverConfig struct {
API string `yaml:"api" json:"api"`
Intraservice string `yaml:"intraservice" json:"intraservice"`
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (a *AuthKeyserver) UnmarshalYAML(f func(interface{}) error) error {
var m keyserverConfig
if err := f(&m); err != nil {
return nil
}
a.API = m.API
s, err := base64.StdEncoding.DecodeString(m.Intraservice)
if err != nil {
return err
}
a.Intraservice = s
return nil
}

// MarshalYAML implements yaml.Marshaler.
func (a *AuthKeyserver) MarshalYAML() (interface{}, error) {
return &keyserverConfig{
API: a.API,
Intraservice: base64.StdEncoding.EncodeToString(a.Intraservice),
}, nil
Intraservice Base64 `yaml:"intraservice" json:"intraservice"`
}

// AuthPSK is the configuration for doing pre-shared key based authentication.
//
// The "Issuer" key is what the service expects to verify as the "issuer" claim.
type AuthPSK struct {
Key []byte `yaml:"key" json:"key"`
Issuer []string `yaml:"iss" json:"iss"`
}
type pskConfig struct {
Key string `yaml:"key" json:"key"`
Key Base64 `yaml:"key" json:"key"`
Issuer []string `yaml:"iss" json:"iss"`
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (a *AuthPSK) UnmarshalYAML(f func(interface{}) error) error {
var m pskConfig
if err := f(&m); err != nil {
return nil
}
a.Issuer = m.Issuer
s, err := base64.StdEncoding.DecodeString(m.Key)
if err != nil {
return err
}
a.Key = s
return nil
}

// MarshalYAML implements yaml.Marshaler.
func (a *AuthPSK) MarshalYAML() (interface{}, error) {
return &pskConfig{
Key: base64.StdEncoding.EncodeToString(a.Key),
Issuer: a.Issuer,
}, nil
}
4 changes: 2 additions & 2 deletions config/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func (cfg *Config) Client(next http.RoundTripper, cl *jwt.Claims) (c *http.Clien
switch {
case cl == nil: // Skip signing
case cfg.Auth.Keyserver != nil:
sk.Key = cfg.Auth.Keyserver.Intraservice
sk.Key = []byte(cfg.Auth.Keyserver.Intraservice)
case cfg.Auth.PSK != nil:
sk.Key = cfg.Auth.PSK.Key
sk.Key = []byte(cfg.Auth.PSK.Key)
default:
}
rt := &transport{
Expand Down

0 comments on commit ae7e5a3

Please sign in to comment.