2016-09-27 02:48:16 -04:00
|
|
|
package controlapi
|
|
|
|
|
|
|
|
import (
|
2016-11-16 21:15:15 -05:00
|
|
|
"crypto/subtle"
|
2016-10-03 13:58:05 -04:00
|
|
|
"regexp"
|
2016-11-04 15:11:41 -04:00
|
|
|
"strings"
|
2016-10-03 13:58:05 -04:00
|
|
|
|
2016-11-02 14:43:27 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-10-03 13:58:05 -04:00
|
|
|
"github.com/docker/distribution/digest"
|
2016-09-27 02:48:16 -04:00
|
|
|
"github.com/docker/swarmkit/api"
|
2016-10-03 13:58:05 -04:00
|
|
|
"github.com/docker/swarmkit/identity"
|
2016-11-02 14:43:27 -04:00
|
|
|
"github.com/docker/swarmkit/log"
|
2016-10-03 13:58:05 -04:00
|
|
|
"github.com/docker/swarmkit/manager/state/store"
|
2016-09-27 02:48:16 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Currently this is contains the unimplemented secret functions in order to satisfy the interface
|
|
|
|
|
|
|
|
// MaxSecretSize is the maximum byte length of the `Secret.Spec.Data` field.
|
|
|
|
const MaxSecretSize = 500 * 1024 // 500KB
|
|
|
|
|
2016-10-03 13:58:05 -04:00
|
|
|
var validSecretNameRegexp = regexp.MustCompile(`^[a-zA-Z0-9]+(?:[a-zA-Z0-9-_.]*[a-zA-Z0-9])?$`)
|
|
|
|
|
|
|
|
// assumes spec is not nil
|
|
|
|
func secretFromSecretSpec(spec *api.SecretSpec) *api.Secret {
|
|
|
|
return &api.Secret{
|
|
|
|
ID: identity.NewID(),
|
|
|
|
Spec: *spec,
|
|
|
|
SecretSize: int64(len(spec.Data)),
|
|
|
|
Digest: digest.FromBytes(spec.Data).String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 02:48:16 -04:00
|
|
|
// GetSecret returns a `GetSecretResponse` with a `Secret` with the same
|
|
|
|
// id as `GetSecretRequest.SecretID`
|
|
|
|
// - Returns `NotFound` if the Secret with the given id is not found.
|
|
|
|
// - Returns `InvalidArgument` if the `GetSecretRequest.SecretID` is empty.
|
|
|
|
// - Returns an error if getting fails.
|
|
|
|
func (s *Server) GetSecret(ctx context.Context, request *api.GetSecretRequest) (*api.GetSecretResponse, error) {
|
2016-10-03 13:58:05 -04:00
|
|
|
if request.SecretID == "" {
|
|
|
|
return nil, grpc.Errorf(codes.InvalidArgument, "secret ID must be provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
var secret *api.Secret
|
|
|
|
s.store.View(func(tx store.ReadTx) {
|
|
|
|
secret = store.GetSecret(tx, request.SecretID)
|
|
|
|
})
|
|
|
|
|
|
|
|
if secret == nil {
|
|
|
|
return nil, grpc.Errorf(codes.NotFound, "secret %s not found", request.SecretID)
|
|
|
|
}
|
|
|
|
|
2016-10-26 09:35:48 -04:00
|
|
|
secret.Spec.Data = nil // clean the actual secret data so it's never returned
|
2016-10-03 13:58:05 -04:00
|
|
|
return &api.GetSecretResponse{Secret: secret}, nil
|
2016-09-27 02:48:16 -04:00
|
|
|
}
|
|
|
|
|
2016-10-26 09:35:48 -04:00
|
|
|
// UpdateSecret updates a Secret referenced by SecretID with the given SecretSpec.
|
|
|
|
// - Returns `NotFound` if the Secret is not found.
|
|
|
|
// - Returns `InvalidArgument` if the SecretSpec is malformed or anything other than Labels is changed
|
|
|
|
// - Returns an error if the update fails.
|
|
|
|
func (s *Server) UpdateSecret(ctx context.Context, request *api.UpdateSecretRequest) (*api.UpdateSecretResponse, error) {
|
|
|
|
if request.SecretID == "" || request.SecretVersion == nil {
|
|
|
|
return nil, grpc.Errorf(codes.InvalidArgument, errInvalidArgument.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var secret *api.Secret
|
|
|
|
err := s.store.Update(func(tx store.Tx) error {
|
|
|
|
secret = store.GetSecret(tx, request.SecretID)
|
|
|
|
if secret == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-16 21:15:15 -05:00
|
|
|
// Check if the Name is different than the current name, or the secret is non-nil and different
|
|
|
|
// than the current secret
|
|
|
|
if secret.Spec.Annotations.Name != request.Spec.Annotations.Name ||
|
|
|
|
(request.Spec.Data != nil && subtle.ConstantTimeCompare(request.Spec.Data, secret.Spec.Data) == 0) {
|
2016-10-26 09:35:48 -04:00
|
|
|
return grpc.Errorf(codes.InvalidArgument, "only updates to Labels are allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only allow updating Labels
|
|
|
|
secret.Meta.Version = *request.SecretVersion
|
|
|
|
secret.Spec.Annotations.Labels = request.Spec.Annotations.Labels
|
|
|
|
|
|
|
|
return store.UpdateSecret(tx, secret)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if secret == nil {
|
|
|
|
return nil, grpc.Errorf(codes.NotFound, "secret %s not found", request.SecretID)
|
|
|
|
}
|
|
|
|
|
2016-11-02 14:43:27 -04:00
|
|
|
log.G(ctx).WithFields(logrus.Fields{
|
|
|
|
"secret.ID": request.SecretID,
|
|
|
|
"secret.Name": request.Spec.Annotations.Name,
|
|
|
|
"method": "UpdateSecret",
|
|
|
|
}).Debugf("secret updated")
|
|
|
|
|
2016-10-26 09:35:48 -04:00
|
|
|
// WARN: we should never return the actual secret data here. We need to redact the private fields first.
|
|
|
|
secret.Spec.Data = nil
|
|
|
|
return &api.UpdateSecretResponse{
|
|
|
|
Secret: secret,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-09-27 02:48:16 -04:00
|
|
|
// ListSecrets returns a `ListSecretResponse` with a list all non-internal `Secret`s being
|
|
|
|
// managed, or all secrets matching any name in `ListSecretsRequest.Names`, any
|
|
|
|
// name prefix in `ListSecretsRequest.NamePrefixes`, any id in
|
|
|
|
// `ListSecretsRequest.SecretIDs`, or any id prefix in `ListSecretsRequest.IDPrefixes`.
|
|
|
|
// - Returns an error if listing fails.
|
|
|
|
func (s *Server) ListSecrets(ctx context.Context, request *api.ListSecretsRequest) (*api.ListSecretsResponse, error) {
|
2016-10-03 13:58:05 -04:00
|
|
|
var (
|
|
|
|
secrets []*api.Secret
|
|
|
|
respSecrets []*api.Secret
|
|
|
|
err error
|
|
|
|
byFilters []store.By
|
|
|
|
by store.By
|
|
|
|
labels map[string]string
|
|
|
|
)
|
|
|
|
|
|
|
|
// return all secrets that match either any of the names or any of the name prefixes (why would you give both?)
|
|
|
|
if request.Filters != nil {
|
|
|
|
for _, name := range request.Filters.Names {
|
|
|
|
byFilters = append(byFilters, store.ByName(name))
|
|
|
|
}
|
|
|
|
for _, prefix := range request.Filters.NamePrefixes {
|
|
|
|
byFilters = append(byFilters, store.ByNamePrefix(prefix))
|
|
|
|
}
|
|
|
|
for _, prefix := range request.Filters.IDPrefixes {
|
|
|
|
byFilters = append(byFilters, store.ByIDPrefix(prefix))
|
|
|
|
}
|
|
|
|
labels = request.Filters.Labels
|
|
|
|
}
|
|
|
|
|
|
|
|
switch len(byFilters) {
|
|
|
|
case 0:
|
|
|
|
by = store.All
|
|
|
|
case 1:
|
|
|
|
by = byFilters[0]
|
|
|
|
default:
|
|
|
|
by = store.Or(byFilters...)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.store.View(func(tx store.ReadTx) {
|
|
|
|
secrets, err = store.FindSecrets(tx, by)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// strip secret data from the secret, filter by label, and filter out all internal secrets
|
|
|
|
for _, secret := range secrets {
|
|
|
|
if secret.Internal || !filterMatchLabels(secret.Spec.Annotations.Labels, labels) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
secret.Spec.Data = nil // clean the actual secret data so it's never returned
|
|
|
|
respSecrets = append(respSecrets, secret)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.ListSecretsResponse{Secrets: respSecrets}, nil
|
2016-09-27 02:48:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSecret creates and return a `CreateSecretResponse` with a `Secret` based
|
|
|
|
// on the provided `CreateSecretRequest.SecretSpec`.
|
|
|
|
// - Returns `InvalidArgument` if the `CreateSecretRequest.SecretSpec` is malformed,
|
|
|
|
// or if the secret data is too long or contains invalid characters.
|
|
|
|
// - Returns an error if the creation fails.
|
|
|
|
func (s *Server) CreateSecret(ctx context.Context, request *api.CreateSecretRequest) (*api.CreateSecretResponse, error) {
|
2016-10-03 13:58:05 -04:00
|
|
|
if err := validateSecretSpec(request.Spec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
secret := secretFromSecretSpec(request.Spec) // the store will handle name conflicts
|
|
|
|
err := s.store.Update(func(tx store.Tx) error {
|
|
|
|
return store.CreateSecret(tx, secret)
|
|
|
|
})
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
case store.ErrNameConflict:
|
|
|
|
return nil, grpc.Errorf(codes.AlreadyExists, "secret %s already exists", request.Spec.Annotations.Name)
|
|
|
|
case nil:
|
|
|
|
secret.Spec.Data = nil // clean the actual secret data so it's never returned
|
2016-11-02 14:43:27 -04:00
|
|
|
log.G(ctx).WithFields(logrus.Fields{
|
|
|
|
"secret.Name": request.Spec.Annotations.Name,
|
|
|
|
"method": "CreateSecret",
|
|
|
|
}).Debugf("secret created")
|
|
|
|
|
2016-10-03 13:58:05 -04:00
|
|
|
return &api.CreateSecretResponse{Secret: secret}, nil
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-27 02:48:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveSecret removes the secret referenced by `RemoveSecretRequest.ID`.
|
|
|
|
// - Returns `InvalidArgument` if `RemoveSecretRequest.ID` is empty.
|
|
|
|
// - Returns `NotFound` if the a secret named `RemoveSecretRequest.ID` is not found.
|
2016-11-04 15:11:41 -04:00
|
|
|
// - Returns `SecretInUse` if the secret is currently in use
|
2016-09-27 02:48:16 -04:00
|
|
|
// - Returns an error if the deletion fails.
|
|
|
|
func (s *Server) RemoveSecret(ctx context.Context, request *api.RemoveSecretRequest) (*api.RemoveSecretResponse, error) {
|
2016-10-03 13:58:05 -04:00
|
|
|
if request.SecretID == "" {
|
|
|
|
return nil, grpc.Errorf(codes.InvalidArgument, "secret ID must be provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.store.Update(func(tx store.Tx) error {
|
2016-11-04 15:11:41 -04:00
|
|
|
// Check if the secret exists
|
|
|
|
secret := store.GetSecret(tx, request.SecretID)
|
|
|
|
if secret == nil {
|
|
|
|
return grpc.Errorf(codes.NotFound, "could not find secret %s", request.SecretID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any services currently reference this secret, return error if so
|
|
|
|
services, err := store.FindServices(tx, store.ByReferencedSecretID(request.SecretID))
|
|
|
|
if err != nil {
|
|
|
|
return grpc.Errorf(codes.Internal, "could not find services using secret %s: %v", request.SecretID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(services) != 0 {
|
|
|
|
serviceNames := make([]string, 0, len(services))
|
|
|
|
for _, service := range services {
|
|
|
|
serviceNames = append(serviceNames, service.Spec.Annotations.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
secretName := secret.Spec.Annotations.Name
|
|
|
|
serviceNameStr := strings.Join(serviceNames, ", ")
|
|
|
|
serviceStr := "services"
|
|
|
|
if len(serviceNames) == 1 {
|
|
|
|
serviceStr = "service"
|
|
|
|
}
|
|
|
|
|
|
|
|
return grpc.Errorf(codes.InvalidArgument, "secret '%s' is in use by the following %s: %v", secretName, serviceStr, serviceNameStr)
|
|
|
|
}
|
|
|
|
|
2016-10-03 13:58:05 -04:00
|
|
|
return store.DeleteSecret(tx, request.SecretID)
|
|
|
|
})
|
|
|
|
switch err {
|
|
|
|
case store.ErrNotExist:
|
|
|
|
return nil, grpc.Errorf(codes.NotFound, "secret %s not found", request.SecretID)
|
|
|
|
case nil:
|
2016-11-02 14:43:27 -04:00
|
|
|
log.G(ctx).WithFields(logrus.Fields{
|
|
|
|
"secret.ID": request.SecretID,
|
|
|
|
"method": "RemoveSecret",
|
|
|
|
}).Debugf("secret removed")
|
|
|
|
|
2016-10-03 13:58:05 -04:00
|
|
|
return &api.RemoveSecretResponse{}, nil
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateSecretSpec(spec *api.SecretSpec) error {
|
|
|
|
if spec == nil {
|
|
|
|
return grpc.Errorf(codes.InvalidArgument, errInvalidArgument.Error())
|
|
|
|
}
|
|
|
|
if err := validateSecretAnnotations(spec.Annotations); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(spec.Data) >= MaxSecretSize || len(spec.Data) < 1 {
|
|
|
|
return grpc.Errorf(codes.InvalidArgument, "secret data must be larger than 0 and less than %d bytes", MaxSecretSize)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateSecretAnnotations(m api.Annotations) error {
|
|
|
|
if m.Name == "" {
|
|
|
|
return grpc.Errorf(codes.InvalidArgument, "name must be provided")
|
|
|
|
} else if len(m.Name) > 64 || !validSecretNameRegexp.MatchString(m.Name) {
|
|
|
|
// if the name doesn't match the regex
|
|
|
|
return grpc.Errorf(codes.InvalidArgument,
|
|
|
|
"invalid name, only 64 [a-zA-Z0-9-_.] characters allowed, and the start and end character must be [a-zA-Z0-9]")
|
|
|
|
}
|
|
|
|
return nil
|
2016-09-27 02:48:16 -04:00
|
|
|
}
|