Skip to content

Commit

Permalink
feat(yaml/secret): adding pull tag to secrets to create a lazy secret…
Browse files Browse the repository at this point in the history
…s ability (#312)

* feat: adding pull tag to secrets to create a lazy secrets ability

* fixing testing errors

* fixing testing errors

* changing pull tag options

* fixing tests

* fixing linter errors

* fixing some errors

* fixing formatting

* fixing errors

---------

Co-authored-by: Claire.Nicholas <[email protected]>
Co-authored-by: Tim Huynh <[email protected]>
Co-authored-by: David May <[email protected]>
Co-authored-by: Easton Crupper <[email protected]>
  • Loading branch information
5 people authored Oct 24, 2023
1 parent 0c0b890 commit 19101a5
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions constants/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ package constants

// Secret types.
const (
// SecretPullBuild defines the pull policy type for a secret.
SecretPullBuild = "build_start"

// SecretPullStep defines the pull policy type for a secret.
SecretPullStep = "step_start"

// SecretOrg defines the secret type for a secret scoped to a specific org.
SecretOrg = "org"

Expand Down
1 change: 1 addition & 0 deletions pipeline/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type (
Engine string `json:"engine,omitempty" yaml:"engine,omitempty"`
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Origin *Container `json:"origin,omitempty" yaml:"origin,omitempty"`
Pull string `json:"pull,omitempty" yaml:"pull,omitempty"`
}

// StepSecretSlice is the pipeline representation
Expand Down
30 changes: 30 additions & 0 deletions pipeline/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestPipeline_Secret_ParseOrg_success(t *testing.T) {
Key: "octocat/foo",
Engine: "native",
Type: "org",
Pull: "build_start",
},
org: "octocat",
},
Expand All @@ -70,6 +71,7 @@ func TestPipeline_Secret_ParseOrg_success(t *testing.T) {
Key: "octocat/👋/🧪/🔑",
Engine: "native",
Type: "org",
Pull: "build_start",
},
org: "octocat",
},
Expand Down Expand Up @@ -108,6 +110,7 @@ func TestPipeline_Secret_ParseOrg_failure(t *testing.T) {
Key: "octocat/foo",
Engine: "native",
Type: "org",
Pull: "build_start",
},
org: "wrongorg",
wantErr: ErrInvalidOrg,
Expand All @@ -119,6 +122,7 @@ func TestPipeline_Secret_ParseOrg_failure(t *testing.T) {
Key: "octocat",
Engine: "native",
Type: "org",
Pull: "build_start",
},
org: "octocat",
wantErr: ErrInvalidPath,
Expand All @@ -130,6 +134,7 @@ func TestPipeline_Secret_ParseOrg_failure(t *testing.T) {
Key: "octocat/",
Engine: "native",
Type: "org",
Pull: "build_start",
},
org: "octocat",
wantErr: ErrInvalidPath,
Expand All @@ -140,6 +145,7 @@ func TestPipeline_Secret_ParseOrg_failure(t *testing.T) {
Key: "octocat/foo/bar",
Engine: "native",
Type: "org",
Pull: "build_start",
},
org: "octocat",
wantErr: ErrInvalidName,
Expand All @@ -151,6 +157,7 @@ func TestPipeline_Secret_ParseOrg_failure(t *testing.T) {
Key: "octocat/foo/bar",
Engine: "native",
Type: "org",
Pull: "build_start",
},
org: "octocat",
wantErr: ErrInvalidName,
Expand All @@ -162,6 +169,7 @@ func TestPipeline_Secret_ParseOrg_failure(t *testing.T) {
Key: "octocat/foo",
Engine: "invalid",
Type: "org",
Pull: "build_start",
},
org: "octocat",
wantErr: ErrInvalidEngine,
Expand Down Expand Up @@ -195,6 +203,7 @@ func TestPipeline_Secret_ParseRepo_success(t *testing.T) {
Key: "octocat/helloworld/foo",
Engine: "native",
Type: "repo",
Pull: "build_start",
},
org: "octocat",
repo: "helloworld",
Expand All @@ -206,6 +215,7 @@ func TestPipeline_Secret_ParseRepo_success(t *testing.T) {
Key: "octocat/👋/🧪/🔑",
Engine: "native",
Type: "repo",
Pull: "build_start",
},
org: "octocat",
repo: "👋",
Expand Down Expand Up @@ -253,6 +263,7 @@ func TestPipeline_Secret_ParseRepo_failure(t *testing.T) {
Key: "octocat/helloworld/foo",
Engine: "native",
Type: "repo",
Pull: "build_start",
},
org: "wrongorg",
repo: "helloworld",
Expand All @@ -265,6 +276,7 @@ func TestPipeline_Secret_ParseRepo_failure(t *testing.T) {
Key: "octocat/helloworld/foo",
Engine: "native",
Type: "repo",
Pull: "build_start",
},
org: "octocat",
repo: "badrepo",
Expand All @@ -277,6 +289,7 @@ func TestPipeline_Secret_ParseRepo_failure(t *testing.T) {
Key: "octocat",
Engine: "native",
Type: "repo",
Pull: "build_start",
},
org: "octocat",
wantErr: ErrInvalidPath,
Expand All @@ -288,6 +301,7 @@ func TestPipeline_Secret_ParseRepo_failure(t *testing.T) {
Key: "octocat/helloworld",
Engine: "native",
Type: "org",
Pull: "build_start",
},
repo: "helloworld",
org: "octocat",
Expand All @@ -300,6 +314,7 @@ func TestPipeline_Secret_ParseRepo_failure(t *testing.T) {
Key: "octocat/helloworld/",
Engine: "native",
Type: "org",
Pull: "build_start",
},
repo: "helloworld",
org: "octocat",
Expand All @@ -311,6 +326,7 @@ func TestPipeline_Secret_ParseRepo_failure(t *testing.T) {
Key: "octocat/helloworld/foo/bar",
Engine: "native",
Type: "repo",
Pull: "build_start",
},
org: "octocat",
repo: "helloworld",
Expand All @@ -323,6 +339,7 @@ func TestPipeline_Secret_ParseRepo_failure(t *testing.T) {
Key: "octocat/helloworld/foo/bar",
Engine: "native",
Type: "repo",
Pull: "build_start",
},
org: "octocat",
repo: "helloworld",
Expand All @@ -335,6 +352,7 @@ func TestPipeline_Secret_ParseRepo_failure(t *testing.T) {
Key: "octocat",
Engine: "invalid",
Type: "org",
Pull: "build_start",
},
org: "octocat",
wantErr: ErrInvalidEngine,
Expand All @@ -346,6 +364,7 @@ func TestPipeline_Secret_ParseRepo_failure(t *testing.T) {
Key: "foo",
Engine: "native",
Type: "repo",
Pull: "build_start",
},
org: "octocat",
repo: "helloworld",
Expand Down Expand Up @@ -379,6 +398,7 @@ func TestPipeline_Secret_ParseShared_success(t *testing.T) {
Key: "octocat/helloworld/foo",
Engine: "native",
Type: "repo",
Pull: "build_start",
},
org: "octocat",
},
Expand All @@ -389,6 +409,7 @@ func TestPipeline_Secret_ParseShared_success(t *testing.T) {
Key: "octocat/👋/🧪/🔑",
Engine: "native",
Type: "repo",
Pull: "build_start",
},
org: "octocat",
},
Expand Down Expand Up @@ -431,6 +452,7 @@ func TestPipeline_Secret_ParseShared_failure(t *testing.T) {
Key: "octocat",
Engine: "native",
Type: "repo",
Pull: "build_start",
},
org: "octocat",
wantErr: ErrInvalidPath,
Expand All @@ -442,6 +464,7 @@ func TestPipeline_Secret_ParseShared_failure(t *testing.T) {
Key: "octocat",
Engine: "invalid",
Type: "org",
Pull: "build_start",
},
org: "octocat",
wantErr: ErrInvalidEngine,
Expand All @@ -453,6 +476,7 @@ func TestPipeline_Secret_ParseShared_failure(t *testing.T) {
Key: "octocat/foo",
Engine: "native",
Type: "org",
Pull: "build_start",
},
org: "octocat",
wantErr: ErrInvalidPath,
Expand All @@ -464,6 +488,7 @@ func TestPipeline_Secret_ParseShared_failure(t *testing.T) {
Key: "octocat/foo/",
Engine: "native",
Type: "org",
Pull: "build_start",
},
org: "octocat",
wantErr: ErrInvalidPath,
Expand All @@ -474,6 +499,7 @@ func TestPipeline_Secret_ParseShared_failure(t *testing.T) {
Key: "octocat/foo/bar",
Engine: "native",
Type: "org",
Pull: "build_start",
},
org: "octocat",
wantErr: ErrInvalidName,
Expand All @@ -485,6 +511,7 @@ func TestPipeline_Secret_ParseShared_failure(t *testing.T) {
Key: "octocat/foo/bar",
Engine: "native",
Type: "org",
Pull: "build_start",
},
org: "octocat",
wantErr: ErrInvalidName,
Expand Down Expand Up @@ -512,20 +539,23 @@ func testSecrets() *SecretSlice {
Name: "foobar",
Type: "repo",
Origin: &Container{},
Pull: "build_start",
},
{
Engine: "native",
Key: "github/foobar",
Name: "foobar",
Type: "org",
Origin: &Container{},
Pull: "build_start",
},
{
Engine: "native",
Key: "github/octokitties/foobar",
Name: "foobar",
Type: "shared",
Origin: &Container{},
Pull: "build_start",
},
{
Name: "",
Expand Down
6 changes: 6 additions & 0 deletions yaml/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,36 +260,42 @@ func TestYaml_Build_UnmarshalYAML(t *testing.T) {
Key: "org/repo/docker/username",
Engine: "native",
Type: "repo",
Pull: "build_start",
},
{
Name: "docker_password",
Key: "org/repo/docker/password",
Engine: "vault",
Type: "repo",
Pull: "build_start",
},
{
Name: "docker_username",
Key: "org/docker/username",
Engine: "native",
Type: "org",
Pull: "build_start",
},
{
Name: "docker_password",
Key: "org/docker/password",
Engine: "vault",
Type: "org",
Pull: "build_start",
},
{
Name: "docker_username",
Key: "org/team/docker/username",
Engine: "native",
Type: "shared",
Pull: "build_start",
},
{
Name: "docker_password",
Key: "org/team/docker/password",
Engine: "vault",
Type: "shared",
Pull: "build_start",
},
{
Origin: Origin{
Expand Down
7 changes: 7 additions & 0 deletions yaml/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type (
Engine string `yaml:"engine,omitempty" json:"engine,omitempty" jsonschema:"enum=native,enum=vault,default=native,description=Name of storage backend to fetch secret from.\nReference: https://go-vela.github.io/docs/reference/yaml/secrets/#the-engine-tag"`
Type string `yaml:"type,omitempty" json:"type,omitempty" jsonschema:"enum=repo,enum=org,enum=shared,default=repo,description=Type of secret to fetch from storage backend.\nReference: https://go-vela.github.io/docs/reference/yaml/secrets/#the-type-tag"`
Origin Origin `yaml:"origin,omitempty" json:"origin,omitempty" jsonschema:"description=Declaration to pull secrets from non-internal secret providers.\nReference: https://go-vela.github.io/docs/reference/yaml/secrets/#the-origin-tag"`
Pull string `yaml:"pull,omitempty" json:"pull,omitempty" jsonschema:"default=build_start,description=When to pull in secrets from storage backend."`
}

// Origin is the yaml representation of a method
Expand Down Expand Up @@ -55,6 +56,7 @@ func (s *SecretSlice) ToPipeline() *pipeline.SecretSlice {
Engine: secret.Engine,
Type: secret.Type,
Origin: secret.Origin.ToPipeline(),
Pull: secret.Pull,
})
}

Expand Down Expand Up @@ -94,6 +96,11 @@ func (s *SecretSlice) UnmarshalYAML(unmarshal func(interface{}) error) error {
secret.Type = constants.SecretRepo
}

// implicitly set `type` field if empty
if secret.Origin.Empty() && len(secret.Pull) == 0 {
secret.Pull = constants.SecretPullBuild
}

// implicitly set `pull` field if empty
if !secret.Origin.Empty() && len(secret.Origin.Pull) == 0 {
secret.Origin.Pull = constants.PullNotPresent
Expand Down
Loading

0 comments on commit 19101a5

Please sign in to comment.