From bd277c469de21464889f28baee9cd5fe0f29d05b Mon Sep 17 00:00:00 2001 From: Andreas Auernhammer Date: Thu, 11 Jan 2024 14:09:29 +0100 Subject: [PATCH] remove unused code (#432) This commit removes the unused `internal/yml` package. Signed-off-by: Andreas Auernhammer --- internal/cli/init.go | 81 --------------- internal/cli/init_test.go | 15 --- internal/cli/testdata/env.yml | 30 ------ internal/yml/types.go | 188 ---------------------------------- 4 files changed, 314 deletions(-) delete mode 100644 internal/cli/testdata/env.yml delete mode 100644 internal/yml/types.go diff --git a/internal/cli/init.go b/internal/cli/init.go index c730f9d8..5b1028e4 100644 --- a/internal/cli/init.go +++ b/internal/cli/init.go @@ -6,92 +6,11 @@ package cli import ( "errors" - "os" "strconv" - "github.com/minio/kes/internal/yml" "gopkg.in/yaml.v3" ) -// InitConfig is a structure containing all -// possible KES initialization configuration -// fields. -type InitConfig struct { - Version string `yaml:"version"` - - Address yml.String `yaml:"address"` - - System struct { - Admin struct { - Identity yml.Identity `yaml:"identity"` - } `yaml:"admin"` - } `yaml:"system"` - - TLS struct { - PrivateKey yml.String `yaml:"key"` - Certificate yml.String `yaml:"cert"` - Password yml.String `yaml:"password"` - - Proxy struct { - Identity []yml.Identity `yaml:"identity"` - Header struct { - ClientCert yml.String `yaml:"cert"` - } `yaml:"header"` - } `yaml:"proxy"` - - Client struct { - VerifyCerts yml.Bool `yaml:"verify_cert"` - } `yaml:"client"` - } `yaml:"tls"` - - Unseal struct { - Environment struct { - Name string `yaml:"name"` - } `yaml:"environment"` - } `yaml:"unseal"` - - Enclave map[string]struct { - Admin struct { - Identity yml.Identity `yaml:"identity"` - } `yaml:"admin"` - - Policy map[string]struct { - Allow []string `yaml:"allow"` - Deny []string `yaml:"deny"` - Identity []yml.Identity `yaml:"identities"` - } `yaml:"policy"` - } `yaml:"enclave"` -} - -// ReadInitConfig reads and parses the InitConfig YAML representation -// from the given file. -func ReadInitConfig(filename string) (*InitConfig, error) { - f, err := os.Open(filename) - if err != nil { - return nil, err - } - defer f.Close() - - var node yaml.Node - if err := yaml.NewDecoder(f).Decode(&node); err != nil { - return nil, err - } - - version, err := findVersion(&node) - if err != nil { - return nil, err - } - if version != "v1" { - return nil, errors.New("cli: invalid init config version '" + version + "'") - } - - var config InitConfig - if err := node.Decode(&config); err != nil { - return nil, err - } - return &config, nil -} - // findVersion finds the version field in the // the given YAML document AST. // diff --git a/internal/cli/init_test.go b/internal/cli/init_test.go index 91e63cd4..0d92dce8 100644 --- a/internal/cli/init_test.go +++ b/internal/cli/init_test.go @@ -10,21 +10,6 @@ import ( "gopkg.in/yaml.v3" ) -func TestReadInitConfig(t *testing.T) { - for i, test := range readInitConfigTests { - _, err := ReadInitConfig(test.Filename) - if err != nil { - t.Fatalf("Test %d: failed to read init config: %v", i, err) - } - } -} - -var readInitConfigTests = []struct { - Filename string -}{ - {Filename: "./testdata/env.yml"}, -} - func TestFindVersion(t *testing.T) { for i, test := range findVersionsTests { version, err := findVersion(test.Root) diff --git a/internal/cli/testdata/env.yml b/internal/cli/testdata/env.yml deleted file mode 100644 index 32270fa1..00000000 --- a/internal/cli/testdata/env.yml +++ /dev/null @@ -1,30 +0,0 @@ -version: v1 -address: 0.0.0.0:7373 - -system: - admin: - identity: f4477eea43ff73d05020906cdb9d277dacf638326165ba92eb262531382a9a76 - -tls: - key: ./private.key - cert: ./public.crt - - client: - verify_cert: false - -unseal: - environment: - name: "KES_UNSEAL_KEY" - -enclave: - minio: - admin: - identity: 974540ce5be311df3f29bff18caed43fcdbbeaf32efabee222b4827402699491 - policy: - tenant-1: - allow: - - /v1/key/create/tenant-1* - - /v1/key/generate/tenant-1* - - /v1/key/decrypt/tenant-1* - identities: - - 413c29fe16e7e818a74386c5350ed6781ea4791fd65ce2454568695bd32b95e0 \ No newline at end of file diff --git a/internal/yml/types.go b/internal/yml/types.go deleted file mode 100644 index bbba13ca..00000000 --- a/internal/yml/types.go +++ /dev/null @@ -1,188 +0,0 @@ -// Copyright 2021 - MinIO, Inc. All rights reserved. -// Use of this source code is governed by the AGPLv3 -// license that can be found in the LICENSE file. - -package yml - -import ( - "os" - "strings" - "time" - - "github.com/minio/kes-go" - "gopkg.in/yaml.v3" -) - -var ( // compiler checks - _ yaml.Marshaler = Identity{} - _ yaml.Unmarshaler = (*Identity)(nil) - - _ yaml.Marshaler = String{} - _ yaml.Unmarshaler = (*String)(nil) - - _ yaml.Marshaler = Duration{} - _ yaml.Unmarshaler = (*Duration)(nil) - - _ yaml.Marshaler = Bool{} - _ yaml.Unmarshaler = (*Bool)(nil) -) - -// Identity is a KES identity. It supports YAML -// serialization and deserialization. -// -// During deserialization it replaces env. variable -// references with the corresponding values from -// the environment. -// -// However, it preserves the YAML representation -// and does not serialize any value from the -// environment. -type Identity struct { - raw string - value kes.Identity -} - -// Value returns the KES identity. -func (i *Identity) Value() kes.Identity { return i.value } - -// Set sets the Identity value. -func (i *Identity) Set(value kes.Identity) { i.value = value } - -// MarshalYAML returns the Identity's YAML representation. -func (i Identity) MarshalYAML() (any, error) { return i.raw, nil } - -// UnmarshalYAML uses the unmarhsal function to unmarshal -// a YAML block into the Identity. -func (i *Identity) UnmarshalYAML(node *yaml.Node) error { - var raw string - if err := node.Decode(&raw); err != nil { - return err - } - i.raw, i.value = raw, kes.Identity(replace(raw)) - return nil -} - -// String is a YAML string. It supports YAML -// serialization and deserialization. -// -// During deserialization it replaces env. variable -// references with the corresponding values from -// the environment. -// -// However, it preserves the YAML representation -// and does not serialize any value from the -// environment. -type String struct { - raw string - value string -} - -// Value returns the plain string value. -func (s *String) Value() string { return s.value } - -// Set sets the String value. -func (s *String) Set(value string) { s.value = value } - -// MarshalYAML returns the String's YAML representation. -func (s String) MarshalYAML() (any, error) { return s.raw, nil } - -// UnmarshalYAML uses the unmarhsal function to unmarshal -// a YAML block into the String. -func (s *String) UnmarshalYAML(node *yaml.Node) error { - var raw string - if err := node.Decode(&raw); err != nil { - return err - } - s.raw, s.value = raw, replace(raw) - return nil -} - -// A Duration represents the elapsed time between two instants -// as an int64 nanosecond count. The representation limits the -// largest representable duration to approximately 290 years. -// -// It supports YAML serialization and deserialization. -// -// During deserialization it replaces env. variable references -// with the corresponding values from the environment. -// -// However, it preserves the YAML representation and does not -// serialize any value from the environment. -type Duration struct { - raw string - value time.Duration -} - -// Value returns the time duration value. -func (d *Duration) Value() time.Duration { return d.value } - -// Set sets the Duration value. -func (d *Duration) Set(value time.Duration) { d.value = value } - -// MarshalYAML returns the Duration's YAML representation. -func (d Duration) MarshalYAML() (any, error) { return d.raw, nil } - -// UnmarshalYAML uses the unmarhsal function to unmarshal -// a YAML block into the Duration. -func (d *Duration) UnmarshalYAML(node *yaml.Node) error { - var raw string - if err := node.Decode(&raw); err != nil { - return err - } - value, err := time.ParseDuration(replace(raw)) - if err != nil { - return &yaml.TypeError{Errors: []string{err.Error()}} - } - d.raw, d.value = raw, value - return nil -} - -// Bool is a YAML bool. It supports YAML -// serialization and deserialization. -// -// During deserialization it replaces env. variable -// references with the corresponding values from -// the environment. -// -// However, it preserves the YAML representation -// and does not serialize any value from the -// environment. -type Bool struct { - raw string - value bool -} - -// Value returns the boolean value, either true or false. -func (b *Bool) Value() bool { return b.value } - -// Set sets the boolean value. -func (b *Bool) Set(value bool) { b.value = value } - -// MarshalYAML returns the Bool's YAML representation. -func (b Bool) MarshalYAML() (any, error) { return b.raw, nil } - -// UnmarshalYAML uses the unmarhsal function to unmarshal -// a YAML block into the Bool. -func (b *Bool) UnmarshalYAML(node *yaml.Node) error { - var raw string - if err := node.Decode(&raw); err != nil { - return err - } - switch strings.ToLower(strings.TrimSpace(replace(raw))) { - case "on", "true": - b.raw, b.value = raw, true - return nil - case "off", "false", "": - b.raw, b.value = raw, false - return nil - default: - return &yaml.TypeError{Errors: []string{"invalid value for bool"}} - } -} - -func replace(s string) string { - if t := strings.TrimSpace(s); strings.HasPrefix(t, "${") && strings.HasSuffix(t, "}") { - s = os.ExpandEnv(t) - } - return s -}