harness-drone/plugin/registry/endpoint.go

63 lines
1.5 KiB
Go
Raw Normal View History

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.
// +build !oss
2019-02-19 23:56:41 +00:00
package registry
import (
"context"
"github.com/drone/drone-go/plugin/registry"
"github.com/drone/drone/core"
"github.com/drone/drone/logger"
2019-02-19 23:56:41 +00:00
)
// EndpointSource returns a registry credential provider
// that sources registry credentials from an http endpoint.
func EndpointSource(endpoint, secret string, skipVerify bool) core.RegistryService {
return &service{
endpoint: endpoint,
secret: secret,
skipVerify: skipVerify,
}
}
type service struct {
endpoint string
secret string
skipVerify bool
}
func (c *service) List(ctx context.Context, in *core.RegistryArgs) ([]*core.Registry, error) {
if c.endpoint == "" {
return nil, nil
}
logger := logger.FromContext(ctx)
logger.Trace("registry: plugin: get credentials")
2019-02-19 23:56:41 +00:00
req := &registry.Request{
Repo: toRepo(in.Repo),
Build: toBuild(in.Build),
}
client := registry.Client(c.endpoint, c.secret, c.skipVerify)
res, err := client.List(ctx, req)
if err != nil {
logger.WithError(err).Warn("registry: plugin: cannot get credentials")
2019-02-19 23:56:41 +00:00
return nil, err
}
var registries []*core.Registry
for _, registry := range res {
registries = append(registries, &core.Registry{
Address: registry.Address,
Username: registry.Username,
Password: registry.Password,
})
logger.WithField("address", registry.Address).
Trace("registry: plugin: found credentials")
2019-02-19 23:56:41 +00:00
}
return registries, nil
}