2018-02-05 21:05:59 +00:00
|
|
|
package cluster // import "github.com/docker/docker/daemon/cluster"
|
2016-10-19 16:22:02 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
|
|
|
|
2016-10-19 16:22:02 +00:00
|
|
|
apitypes "github.com/docker/docker/api/types"
|
|
|
|
types "github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/daemon/cluster/convert"
|
2022-04-21 21:33:07 +00:00
|
|
|
swarmapi "github.com/moby/swarmkit/v2/api"
|
2019-06-03 16:33:01 +00:00
|
|
|
"google.golang.org/grpc"
|
2016-10-19 16:22:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetSecret returns a secret from a managed swarm cluster
|
2017-02-11 18:30:53 +00:00
|
|
|
func (c *Cluster) GetSecret(input string) (types.Secret, error) {
|
2017-02-28 10:12:11 +00:00
|
|
|
var secret *swarmapi.Secret
|
|
|
|
|
|
|
|
if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
|
|
|
|
s, err := getSecret(ctx, state.controlClient, input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
secret = s
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2016-10-19 16:22:02 +00:00
|
|
|
return types.Secret{}, err
|
|
|
|
}
|
2016-12-07 19:06:07 +00:00
|
|
|
return convert.SecretFromGRPC(secret), nil
|
2016-10-19 16:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetSecrets returns all secrets of a managed swarm cluster.
|
|
|
|
func (c *Cluster) GetSecrets(options apitypes.SecretListOptions) ([]types.Secret, error) {
|
2016-11-16 22:17:18 +00:00
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
2016-10-19 16:22:02 +00:00
|
|
|
|
2016-11-16 22:17:18 +00:00
|
|
|
state := c.currentNodeState()
|
|
|
|
if !state.IsActiveManager() {
|
|
|
|
return nil, c.errNoManager(state)
|
2016-10-19 16:22:02 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 18:09:13 +00:00
|
|
|
filters, err := newListSecretsFilters(options.Filters)
|
2016-10-19 16:22:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-09-10 00:05:05 +00:00
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
ctx, cancel := c.getRequestContext(ctx)
|
2016-10-19 16:22:02 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2016-11-16 22:17:18 +00:00
|
|
|
r, err := state.controlClient.ListSecrets(ctx,
|
2019-06-03 16:33:01 +00:00
|
|
|
&swarmapi.ListSecretsRequest{Filters: filters},
|
|
|
|
grpc.MaxCallRecvMsgSize(defaultRecvSizeForListResponse),
|
|
|
|
)
|
2016-10-19 16:22:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-14 03:02:28 +00:00
|
|
|
secrets := make([]types.Secret, 0, len(r.Secrets))
|
2016-10-19 16:22:02 +00:00
|
|
|
|
|
|
|
for _, secret := range r.Secrets {
|
|
|
|
secrets = append(secrets, convert.SecretFromGRPC(secret))
|
|
|
|
}
|
|
|
|
|
|
|
|
return secrets, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSecret creates a new secret in a managed swarm cluster.
|
|
|
|
func (c *Cluster) CreateSecret(s types.SecretSpec) (string, error) {
|
2017-02-28 10:12:11 +00:00
|
|
|
var resp *swarmapi.CreateSecretResponse
|
|
|
|
if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
|
|
|
|
secretSpec := convert.SecretSpecToGRPC(s)
|
|
|
|
|
|
|
|
r, err := state.controlClient.CreateSecret(ctx,
|
|
|
|
&swarmapi.CreateSecretRequest{Spec: &secretSpec})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp = r
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2016-10-19 16:22:02 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2017-02-28 10:12:11 +00:00
|
|
|
return resp.Secret.ID, nil
|
2016-10-19 16:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveSecret removes a secret from a managed swarm cluster.
|
2017-02-11 18:30:53 +00:00
|
|
|
func (c *Cluster) RemoveSecret(input string) error {
|
2017-02-28 10:12:11 +00:00
|
|
|
return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
|
|
|
|
secret, err := getSecret(ctx, state.controlClient, input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-19 16:22:02 +00:00
|
|
|
|
2017-02-28 10:12:11 +00:00
|
|
|
req := &swarmapi.RemoveSecretRequest{
|
|
|
|
SecretID: secret.ID,
|
|
|
|
}
|
2016-10-19 16:22:02 +00:00
|
|
|
|
2017-02-28 10:12:11 +00:00
|
|
|
_, err = state.controlClient.RemoveSecret(ctx, req)
|
2016-12-07 19:06:07 +00:00
|
|
|
return err
|
2017-02-28 10:12:11 +00:00
|
|
|
})
|
2016-10-19 16:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateSecret updates a secret in a managed swarm cluster.
|
2016-11-03 18:09:13 +00:00
|
|
|
// Note: this is not exposed to the CLI but is available from the API only
|
2017-02-09 07:06:44 +00:00
|
|
|
func (c *Cluster) UpdateSecret(input string, version uint64, spec types.SecretSpec) error {
|
2017-02-28 10:12:11 +00:00
|
|
|
return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
|
2017-02-09 07:06:44 +00:00
|
|
|
secret, err := getSecret(ctx, state.controlClient, input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-28 10:12:11 +00:00
|
|
|
secretSpec := convert.SecretSpecToGRPC(spec)
|
|
|
|
|
2017-02-09 07:06:44 +00:00
|
|
|
_, err = state.controlClient.UpdateSecret(ctx,
|
2017-02-28 10:12:11 +00:00
|
|
|
&swarmapi.UpdateSecretRequest{
|
2017-02-09 07:06:44 +00:00
|
|
|
SecretID: secret.ID,
|
2017-02-28 10:12:11 +00:00
|
|
|
SecretVersion: &swarmapi.Version{
|
|
|
|
Index: version,
|
|
|
|
},
|
|
|
|
Spec: &secretSpec,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
})
|
2016-10-19 16:22:02 +00:00
|
|
|
}
|