Skip to content

Commit

Permalink
Fixed bug with spec.template.metadata.creationTimestamp null values
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan0x44 committed Mar 8, 2023
1 parent eb2e481 commit 6ee448b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestNormalizeCertURL(t *testing.T) {
expectError bool
}{
{
inURL: "http://example.com",
inURL: "http://example.com/v1/cert.pem",
expectOutURL: expectedOutURL1,
expectOutURLHost: "example.com",
expectError: false,
Expand Down
16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func configure(environment string, configKey string, configValue string) {
}

func rotate(filename string) {
_, environment, err := nameAndEnvFromFilename(filename)
secretName, environment, err := nameAndEnvFromFilename(filename)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
Expand Down Expand Up @@ -185,10 +185,18 @@ func rotate(filename string) {

// TODO: support creating new sealed secrets from scratch
newSecrets := secrets.ToValues()
if len(sealedSecret.Spec.Template.Metadata) == 0 {
timestamp := time.Now().UTC().Format(time.RFC3339)
// TODO: get namespace for new secrets
secretNamespace := ""
sealedSecret.Spec.Template.Metadata = map[string]*string{
"creationTimestamp": &timestamp,
"name": &secretName,
"namespace": &secretNamespace,
}
}
secretYAML, err := createSecretYAML(
sealedSecret.Metadata.Name,
sealedSecret.Metadata.Namespace,
time.Now(),
sealedSecret.Spec.Template.Metadata,
newSecrets,
)
if err != nil {
Expand Down
16 changes: 6 additions & 10 deletions sealedsecret.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ import (
)

type SealedSecret struct {
ApiVersion string `json:"apiVersion" yaml:"apiVersion"`
Kind string `json:"kind" yaml:"kind"`
Metadata struct {
CreationTimestamp time.Time `json:"creationTimestamp,omitempty" yaml:"creationTimestamp,omitempty"`
Name string `json:"name" yaml:"name"`
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
} `json:"metadata" yaml:"metadata"`
Spec struct {
ApiVersion string `json:"apiVersion" yaml:"apiVersion"`
Kind string `json:"kind" yaml:"kind"`
Metadata map[string]*string `json:"metadata" yaml:"metadata"`
Spec struct {
EncryptedData map[string]string `json:"encryptedData,omitempty" yaml:"encryptedData,omitempty"`
Template struct {
Data *map[string]string `json:"data" yaml:"data"`
Metadata map[string]string `json:"metadata" yaml:"metadata"`
Data *map[string]*string `json:"data" yaml:"data"`
Metadata map[string]*string `json:"metadata" yaml:"metadata"`
} `json:"template" yaml:"template"`
} `json:"spec" yaml:"spec"`
}
Expand Down
21 changes: 7 additions & 14 deletions secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,28 @@ import (
"encoding/base64"
"fmt"
"regexp"
"time"

"gopkg.in/yaml.v3"
)

type secretManifest struct {
ApiVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Type string `yaml:"type"`
Data map[string]string `yaml:"data"`
Metadata map[string]string `yaml:"metadata"`
ApiVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Type string `yaml:"type"`
Data map[string]string `yaml:"data"`
Metadata map[string]*string `yaml:"metadata"`
}

func createSecretYAML(
name string,
namespace string,
timestamp time.Time,
metadata map[string]*string,
secrets map[string]string,
) (manifestYAML string, err error) {
manifest := secretManifest{
ApiVersion: "v1",
Kind: "Secret",
Type: "Opaque",
Data: map[string]string{},
Metadata: map[string]string{
"creationTimestamp": timestamp.UTC().Format(time.RFC3339),
"name": name,
"namespace": namespace,
},
Metadata: metadata,
}
for k, v := range secrets {
manifest.Data[k] = base64.StdEncoding.EncodeToString([]byte(v))
Expand Down
14 changes: 5 additions & 9 deletions secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,23 @@ package main
import (
"strings"
"testing"
"time"
)

func TestCreateSecretYAML(t *testing.T) {
// kubectl create secret generic example-secret -o yaml --from-literal=A=B
datetime := "2023-02-22T23:49:39Z"
expect := "" +
"apiVersion: v1\n" +
"kind: Secret\n" +
"type: Opaque\n" +
"data:\n" +
" A: Qg==\n" +
"metadata:\n" +
" creationTimestamp: \"" + datetime + "\"\n" +
" name: example-secret\n" +
" namespace: default\n"
timestamp, err := time.Parse(time.RFC3339, datetime)
if err != nil {
t.Errorf("Unexpected error: %s", err)
" name: example-secret\n"
secretName := "example-secret"
metadata := map[string]*string{
"name": &secretName,
}
got, err := createSecretYAML("example-secret", "default", timestamp, map[string]string{"A": "B"})
got, err := createSecretYAML(metadata, map[string]string{"A": "B"})
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
Expand Down

0 comments on commit 6ee448b

Please sign in to comment.