Have bundleformat package directly under pluginsdk/support

Signed-off-by: Agustín Martínez Fayó <amartinezfayo@gmail.com>
This commit is contained in:
Agustín Martínez Fayó 2023-07-21 12:04:40 -03:00 committed by Marcos Yacob
parent 826fb3444b
commit b8229713c0
2 changed files with 26 additions and 26 deletions

View File

@ -23,6 +23,13 @@ import (
"github.com/spiffe/spire-plugin-sdk/proto/spire/plugin/types"
)
const (
FormatUnset Format = iota
SPIFFE
PEM
JWKS
)
// Formatter formats a bundle in different formats.
type Formatter struct {
bundle *types.Bundle
@ -35,12 +42,21 @@ type Formatter struct {
// Format represents the bundle formats that are supported by the Formatter.
type Format int
const (
FormatUnset Format = iota
SPIFFE
PEM
JWKS
)
// String returns the string name for the bundle format.
func (bundleFormat Format) String() string {
switch bundleFormat {
case FormatUnset:
return "UNSET"
case SPIFFE:
return "spiffe"
case PEM:
return "pem"
case JWKS:
return "jwks"
default:
return fmt.Sprintf("UNKNOWN(%d)", int(bundleFormat))
}
}
// FromString returns the Format corresponding to the provided string.
func FromString(s string) (Format, error) {
@ -56,31 +72,15 @@ func FromString(s string) (Format, error) {
}
}
// NewBundle return a new *Bundle with the *types.Bundle provided.
// NewFormatter return a new *Bundle with the *types.Bundle provided.
// Use the Bytes() function to get a slice of bytes with the bundle formatted in
// the format specified.
func NewBundle(pluginBundle *types.Bundle) *Formatter {
func NewFormatter(pluginBundle *types.Bundle) *Formatter {
return &Formatter{
bundle: pluginBundle,
}
}
// String returns the string name for the bundle format.
func (bundleFormat Format) String() string {
switch bundleFormat {
case FormatUnset:
return "UNSET"
case SPIFFE:
return "spiffe"
case PEM:
return "pem"
case JWKS:
return "jwks"
default:
return fmt.Sprintf("UNKNOWN(%d)", int(bundleFormat))
}
}
// Format returns the bundle in the form of a slice of bytes in
// the chosen format.
func (b *Formatter) Format(format Format) ([]byte, error) {
@ -184,7 +184,7 @@ func (b *Formatter) toSPIFFEBundle() ([]byte, error) {
// FormatBundle returns the bundle in the form of a slice of bytes in
// the chosen format.
func FormatBundle(bundle *types.Bundle, format Format) ([]byte, error) {
return NewBundle(bundle).Format(format)
return NewFormatter(bundle).Format(format)
}
// getAuthorities gets the X.509 authorities and JWT authorities from the

View File

@ -93,7 +93,7 @@ dZglS5kKnYigmwDh+/U=
},
} {
t.Run(tt.name, func(t *testing.T) {
b := NewBundle(tt.bundle)
b := NewFormatter(tt.bundle)
if !proto.Equal(tt.bundle, b.bundle) {
require.Equal(t, tt.bundle, b.bundle)