Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check service secrets existence #316

Merged
merged 2 commits into from Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions loader/validate.go
Expand Up @@ -70,6 +70,12 @@ func checkConsistency(project *types.Project) error {
return errors.Wrap(errdefs.ErrInvalid, fmt.Sprintf("service %q refers to undefined config %s", s.Name, config.Source))
}
}

for _, secret := range s.Secrets {
if _, ok := project.Secrets[secret.Source]; !ok {
return errors.Wrap(errdefs.ErrInvalid, fmt.Sprintf("service %q refers to undefined secret %s", s.Name, secret.Source))
}
}
}

for name, secret := range project.Secrets {
Expand Down
45 changes: 44 additions & 1 deletion loader/validate_test.go
Expand Up @@ -174,7 +174,7 @@ func TestValidateSecret(t *testing.T) {
err := checkConsistency(project)
assert.NilError(t, err)
})
t.Run("uset secret", func(t *testing.T) {
t.Run("unset secret type", func(t *testing.T) {
project := &types.Project{
Secrets: types.Secrets{
"foo": types.SecretConfig{},
Expand All @@ -183,6 +183,49 @@ func TestValidateSecret(t *testing.T) {
err := checkConsistency(project)
assert.Error(t, err, "secret \"foo\" must declare either `file` or `environment`: invalid compose project")
})

t.Run("service secret exist", func(t *testing.T) {
project := &types.Project{
Secrets: types.Secrets{
"foo": types.SecretConfig{
External: types.External{
External: true,
},
},
},
Services: types.Services([]types.ServiceConfig{
{
Name: "myservice",
Image: "scratch",
Secrets: []types.ServiceSecretConfig{
{
Source: "foo",
},
},
},
}),
}
err := checkConsistency(project)
assert.NilError(t, err)
})

t.Run("service secret undefined", func(t *testing.T) {
project := &types.Project{
Services: types.Services([]types.ServiceConfig{
{
Name: "myservice",
Image: "scratch",
Secrets: []types.ServiceSecretConfig{
{
Source: "foo",
},
},
},
}),
}
err := checkConsistency(project)
assert.Error(t, err, `service "myservice" refers to undefined secret foo: invalid compose project`)
})
}

func TestValidateDependsOn(t *testing.T) {
Expand Down
16 changes: 12 additions & 4 deletions types/project.go
Expand Up @@ -258,25 +258,33 @@ func (p *Project) WithoutUnnecessaryResources() {

networks := Networks{}
for k := range requiredNetworks {
networks[k] = p.Networks[k]
if value, ok := p.Networks[k]; ok {
networks[k] = value
}
}
p.Networks = networks

volumes := Volumes{}
for k := range requiredVolumes {
volumes[k] = p.Volumes[k]
if value, ok := p.Volumes[k]; ok {
volumes[k] = value
}
}
p.Volumes = volumes

secrets := Secrets{}
for k := range requiredSecrets {
secrets[k] = p.Secrets[k]
if value, ok := p.Secrets[k]; ok {
secrets[k] = value
}
}
p.Secrets = secrets

configs := Configs{}
for k := range requiredConfigs {
configs[k] = p.Configs[k]
if value, ok := p.Configs[k]; ok {
configs[k] = value
}
}
p.Configs = configs
}
Expand Down