2019-02-19 23:56:41 +00:00
|
|
|
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Drone Non-Commercial License
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/drone/drone/core"
|
2019-02-22 17:01:11 +00:00
|
|
|
"github.com/drone/drone/logger"
|
2019-02-19 23:56:41 +00:00
|
|
|
"github.com/drone/drone/plugin/registry/auths"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Static returns a new static credentials controller.
|
|
|
|
func Static(secrets []*core.Secret) core.RegistryService {
|
|
|
|
return &staticController{secrets: secrets}
|
|
|
|
}
|
|
|
|
|
|
|
|
type staticController struct {
|
|
|
|
secrets []*core.Secret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *staticController) List(ctx context.Context, in *core.RegistryArgs) ([]*core.Registry, error) {
|
2019-02-22 17:01:11 +00:00
|
|
|
static := map[string]*core.Secret{}
|
2019-02-19 23:56:41 +00:00
|
|
|
for _, secret := range c.secrets {
|
2019-02-22 17:01:11 +00:00
|
|
|
static[secret.Name] = secret
|
|
|
|
}
|
|
|
|
|
|
|
|
var results []*core.Registry
|
|
|
|
for _, name := range in.Pipeline.PullSecrets {
|
|
|
|
logger := logger.FromContext(ctx).WithField("name", name)
|
|
|
|
logger.Trace("registry: image_pull_secret: find secret")
|
|
|
|
|
|
|
|
secret, ok := static[name]
|
|
|
|
if !ok {
|
|
|
|
logger.Warn("registry: image_pull_secret: cannot find secret")
|
2019-02-19 23:56:41 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-02-22 17:01:11 +00:00
|
|
|
|
2019-02-19 23:56:41 +00:00
|
|
|
// The secret can be restricted to non-pull request
|
|
|
|
// events. If the secret is restricted, return
|
|
|
|
// empty results.
|
|
|
|
if secret.PullRequest == false &&
|
|
|
|
in.Build.Event == core.EventPullRequest {
|
2019-02-22 17:01:11 +00:00
|
|
|
logger.Trace("registry: image_pull_secret: pull_request access denied")
|
2019-02-19 23:56:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:01:11 +00:00
|
|
|
logger.Trace("registry: image_pull_secret: secret found")
|
|
|
|
parsed, err := auths.ParseString(secret.Data)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Error("registry: image_pull_secret: parsing error")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
results = append(results, parsed...)
|
|
|
|
}
|
|
|
|
return results, nil
|
2019-02-19 23:56:41 +00:00
|
|
|
}
|