Skip to content

Commit

Permalink
Merge pull request #15 from teodor-pripoae/add-encode-certificate-method
Browse files Browse the repository at this point in the history
Encode certs.Certificate as pem when encoded to yaml
  • Loading branch information
moshloop authored Mar 25, 2020
2 parents 6d691b5 + efd1957 commit 4ae1939
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 561 deletions.
8 changes: 8 additions & 0 deletions certs/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ func (c *Certificate) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}

func (c Certificate) MarshalYAML() (interface{}, error) {
cm := &CertificateMarshaller{
CertFile: string(c.EncodedCertificate()),
PrivateKeyFile: string(c.EncodedPrivateKey()),
}
return cm, nil
}

func LoadCertificate(certificate string) ([]byte, error) {
if strings.HasPrefix(certificate, CertificateHeader) {
return []byte(certificate), nil
Expand Down
33 changes: 30 additions & 3 deletions certs/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"io/ioutil"
"testing"

"github.com/pkg/errors"

"github.com/flanksource/yaml"
. "github.com/onsi/gomega"
"gopkg.in/yaml.v2"
"github.com/pkg/errors"
)

type exampleConfig struct {
Expand Down Expand Up @@ -38,6 +37,34 @@ func TestLoadCertificateFromLiteral(t *testing.T) {
g.Expect(cfg.CA.X509.Subject.CommonName).To(Equal("wildcard.literal.flanksource.com"))
}

func TestMarshalCertificate(t *testing.T) {
g := NewWithT(t)

cfg, err := loadConfig("fixtures/literal.yml")
g.Expect(err).ToNot(HaveOccurred())

generatedBytes, err := yaml.Marshal(cfg)
g.Expect(err).ToNot(HaveOccurred())

generatedCfg := struct {
CA *struct {
Cert *string `yaml:"cert,omitempty"`
PrivateKey *string `yaml:"privateKey,omitempty"`
Password *string `yaml:"password,omitempty"`
}
}{}
err = yaml.Unmarshal(generatedBytes, &generatedCfg)
g.Expect(err).ToNot(HaveOccurred())

g.Expect(generatedCfg.CA).ToNot(BeNil())
g.Expect(generatedCfg.CA.Cert).ToNot(BeNil())
g.Expect(generatedCfg.CA.PrivateKey).ToNot(BeNil())
g.Expect(generatedCfg.CA.Password).To(BeNil())

g.Expect(*generatedCfg.CA.Cert).To(Equal(string(cfg.CA.EncodedCertificate())))
g.Expect(*generatedCfg.CA.PrivateKey).To(Equal(string(cfg.CA.EncodedPrivateKey())))
}

func loadConfig(path string) (*exampleConfig, error) {
cfgBytes, err := ioutil.ReadFile(path)
if err != nil {
Expand Down
24 changes: 4 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,15 @@ module github.com/flanksource/commons
go 1.12

require (
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Shopify/ejson v1.2.1 // indirect
github.com/boltdb/bolt v1.3.1 // indirect
github.com/brancz/gojsontoyaml v0.0.0-20191212081931-bf2969bbd742 // indirect
github.com/docker/libkv v0.2.1 // indirect
github.com/dustin/gojson v0.0.0-20160307161227-2e71ec9dd5ad // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/gosimple/slug v1.9.0 // indirect
github.com/flanksource/yaml v0.0.0-20200322131016-b7b2608b8702
github.com/hairyhenderson/gomplate v3.5.0+incompatible
github.com/hairyhenderson/toml v0.3.0 // indirect
github.com/hashicorp/consul/api v1.4.0 // indirect
github.com/hashicorp/go-getter v1.3.1-0.20190906090232-a0f878cb75da
github.com/hashicorp/vault/api v1.0.4 // indirect
github.com/joho/godotenv v1.3.0 // indirect
github.com/hashicorp/vault/api v1.0.4
github.com/onsi/gomega v1.9.0
github.com/pkg/errors v0.8.1
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.4.2
github.com/spf13/afero v1.2.2 // indirect
github.com/ugorji/go v1.1.7 // indirect
github.com/zealic/xignore v0.3.3 // indirect
golang.org/x/build v0.0.0-20190111050920-041ab4dc3f9d // indirect
golang.org/x/crypto v0.0.0-20200117160349-530e935923ad
golang.org/x/crypto v0.0.0-20200317142112-1b76d66859c6
gopkg.in/hairyhenderson/yaml.v2 v2.2.2 // indirect
gopkg.in/yaml.v2 v2.2.8
k8s.io/client-go v11.0.0+incompatible // indirect
)

replace gopkg.in/hairyhenderson/yaml.v2 => github.com/maxaudron/yaml v0.0.0-20190411130442-27c13492fe3c
Loading

0 comments on commit 4ae1939

Please sign in to comment.