Skip to content

Commit

Permalink
update comments, tests and make test chart more minimal
Browse files Browse the repository at this point in the history
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
  • Loading branch information
aryan9600 committed Mar 4, 2022
1 parent ff1ef66 commit 2afbd41
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 154 deletions.
25 changes: 14 additions & 11 deletions helmtestserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ limitations under the License.
package helmtestserver

import (
"fmt"
"crypto/rand"
"encoding/hex"
"os"
"path/filepath"

Expand All @@ -30,7 +31,7 @@ import (
)

const (
KeyRingName = "TestUser"
keyRingName = "TestUser"
)

// NewTempHelmServer returns a HTTP HelmServer with a newly created
Expand Down Expand Up @@ -79,8 +80,9 @@ func (s *HelmServer) PackageChartWithVersion(path, version string) error {
}

// PackageSignedChartWithVersion attempts to package the chart at the given path
// with the given version and sign it using a PGP keyring, to be served by the HelmServer.
// It returns an error in case of a packaging failure.
// with the given version and sign it using a internally generated PGP keyring, to be served
// by the HelmServer. publicKeyPath is the path where the public key should be written to, which
// can be used to verify this chart. It returns an error in case of a packaging failure.
func (s *HelmServer) PackageSignedChartWithVersion(path, version, publicKeyPath string) error {
return s.packageChart(path, version, publicKeyPath)
}
Expand All @@ -90,35 +92,36 @@ func (s *HelmServer) packageChart(path, version, publicKeyPath string) error {
pkg.Destination = s.Root()
pkg.Version = version
if publicKeyPath != "" {
secretKeyPath := fmt.Sprintf("%s/%s", s.Root(), "secret.pgp")
randBytes := make([]byte, 16)
rand.Read(randBytes)
secretKeyPath := filepath.Join(s.Root(), "secret-"+hex.EncodeToString(randBytes)+".pgp")
if err := generateKeyring(secretKeyPath, publicKeyPath); err != nil {
return err
}
defer os.Remove(secretKeyPath)
pkg.Keyring = secretKeyPath
pkg.Key = KeyRingName
pkg.Key = keyRingName
pkg.Sign = true
}
_, err := pkg.Run(path, nil)
return err
}

func generateKeyring(privateKeyPath, publicKeyPath string) error {
entity, err := openpgp.NewEntity(KeyRingName, "", "", nil)
entity, err := openpgp.NewEntity(keyRingName, "", "", nil)
if err != nil {
return err
}
priv, err := os.Create(privateKeyPath)
defer priv.Close()
if err != nil {
return err
}
pub, err := os.Create(publicKeyPath)
defer pub.Close()
if err != nil {
return err
}
defer func() {
priv.Close()
pub.Close()
}()
if err := entity.SerializePrivate(priv, nil); err != nil {
return err
}
Expand Down
15 changes: 8 additions & 7 deletions helmtestserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ package helmtestserver
import (
"fmt"
"os"
"path/filepath"
"testing"

"golang.org/x/crypto/openpgp"
"helm.sh/helm/v3/pkg/downloader"
)

func TestPackageChart(t *testing.T) {
func TestPackageSignedChartWithVersion(t *testing.T) {
server, err := NewTempHelmServer()
defer os.RemoveAll(server.Root())
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(server.Root())
publicKeyPath := fmt.Sprintf("%s/%s", server.Root(), "pub.pgp")
packagedChartPath := fmt.Sprintf("%s/%s", server.Root(), "helmchart-0.1.0.tgz")
if err := server.packageChart("./testdata/helmchart", "0.1.0", publicKeyPath); err != nil {
publicKeyPath := filepath.Join(server.Root(), "pub.pgp")
packagedChartPath := filepath.Join(server.Root(), "helmchart-0.1.0.tgz")
if err := server.PackageSignedChartWithVersion("./testdata/helmchart", "0.1.0", publicKeyPath); err != nil {
t.Fatal(err)
}

Expand All @@ -26,10 +27,10 @@ func TestPackageChart(t *testing.T) {
}

out, err := os.Open(publicKeyPath)
defer out.Close()
if err != nil {
t.Fatal(err)
}
defer out.Close()

if _, err = openpgp.ReadKeyRing(out); err != nil {
t.Fatal(err)
Expand All @@ -46,10 +47,10 @@ func TestPackageChart(t *testing.T) {

func TestGenerateIndex(t *testing.T) {
server, err := NewTempHelmServer()
defer os.RemoveAll(server.Root())
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(server.Root())

if err := server.PackageChartWithVersion("./testdata/helmchart", "0.1.0"); err != nil {
t.Fatal(err)
Expand Down
28 changes: 0 additions & 28 deletions helmtestserver/testdata/helmchart/templates/hpa.yaml

This file was deleted.

61 changes: 0 additions & 61 deletions helmtestserver/testdata/helmchart/templates/ingress.yaml

This file was deleted.

15 changes: 0 additions & 15 deletions helmtestserver/testdata/helmchart/templates/service.yaml

This file was deleted.

12 changes: 0 additions & 12 deletions helmtestserver/testdata/helmchart/templates/serviceaccount.yaml

This file was deleted.

20 changes: 0 additions & 20 deletions helmtestserver/testdata/helmchart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,6 @@ securityContext: {}
# runAsNonRoot: true
# runAsUser: 1000

service:
type: ClusterIP
port: 80

ingress:
enabled: false
className: ""
annotations: {}
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
hosts:
- host: chart-example.local
paths:
- path: /
pathType: ImplementationSpecific
tls: []
# - secretName: chart-example-tls
# hosts:
# - chart-example.local

resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
Expand Down

0 comments on commit 2afbd41

Please sign in to comment.