2016-10-19 12:22:02 -04:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
apitypes "github.com/docker/docker/api/types"
|
|
|
|
types "github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/daemon/cluster/convert"
|
|
|
|
swarmapi "github.com/docker/swarmkit/api"
|
2017-02-28 05:12:11 -05:00
|
|
|
"golang.org/x/net/context"
|
2016-10-19 12:22:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetSecret returns a secret from a managed swarm cluster
|
2017-02-11 13:30:53 -05:00
|
|
|
func (c *Cluster) GetSecret(input string) (types.Secret, error) {
|
2017-02-28 05:12:11 -05: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 12:22:02 -04:00
|
|
|
return types.Secret{}, err
|
|
|
|
}
|
2016-12-07 14:06:07 -05:00
|
|
|
return convert.SecretFromGRPC(secret), nil
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetSecrets returns all secrets of a managed swarm cluster.
|
|
|
|
func (c *Cluster) GetSecrets(options apitypes.SecretListOptions) ([]types.Secret, error) {
|
2016-11-16 17:17:18 -05:00
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
2016-10-19 12:22:02 -04:00
|
|
|
|
2016-11-16 17:17:18 -05:00
|
|
|
state := c.currentNodeState()
|
|
|
|
if !state.IsActiveManager() {
|
|
|
|
return nil, c.errNoManager(state)
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
2016-11-03 14:09:13 -04:00
|
|
|
filters, err := newListSecretsFilters(options.Filters)
|
2016-10-19 12:22:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx, cancel := c.getRequestContext()
|
|
|
|
defer cancel()
|
|
|
|
|
2016-11-16 17:17:18 -05:00
|
|
|
r, err := state.controlClient.ListSecrets(ctx,
|
2016-10-19 12:22:02 -04:00
|
|
|
&swarmapi.ListSecretsRequest{Filters: filters})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-13 23:02:28 -04:00
|
|
|
secrets := make([]types.Secret, 0, len(r.Secrets))
|
2016-10-19 12:22:02 -04: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 05:12:11 -05: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 12:22:02 -04:00
|
|
|
return "", err
|
|
|
|
}
|
2017-02-28 05:12:11 -05:00
|
|
|
return resp.Secret.ID, nil
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveSecret removes a secret from a managed swarm cluster.
|
2017-02-11 13:30:53 -05:00
|
|
|
func (c *Cluster) RemoveSecret(input string) error {
|
2017-02-28 05:12:11 -05: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 12:22:02 -04:00
|
|
|
|
2017-02-28 05:12:11 -05:00
|
|
|
req := &swarmapi.RemoveSecretRequest{
|
|
|
|
SecretID: secret.ID,
|
|
|
|
}
|
2016-10-19 12:22:02 -04:00
|
|
|
|
2017-02-28 05:12:11 -05:00
|
|
|
_, err = state.controlClient.RemoveSecret(ctx, req)
|
2016-12-07 14:06:07 -05:00
|
|
|
return err
|
2017-02-28 05:12:11 -05:00
|
|
|
})
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateSecret updates a secret in a managed swarm cluster.
|
2016-11-03 14:09:13 -04:00
|
|
|
// Note: this is not exposed to the CLI but is available from the API only
|
2017-02-09 02:06:44 -05:00
|
|
|
func (c *Cluster) UpdateSecret(input string, version uint64, spec types.SecretSpec) error {
|
2017-02-28 05:12:11 -05:00
|
|
|
return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
|
2017-02-09 02:06:44 -05:00
|
|
|
secret, err := getSecret(ctx, state.controlClient, input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-28 05:12:11 -05:00
|
|
|
secretSpec := convert.SecretSpecToGRPC(spec)
|
|
|
|
|
2017-02-09 02:06:44 -05:00
|
|
|
_, err = state.controlClient.UpdateSecret(ctx,
|
2017-02-28 05:12:11 -05:00
|
|
|
&swarmapi.UpdateSecretRequest{
|
2017-02-09 02:06:44 -05:00
|
|
|
SecretID: secret.ID,
|
2017-02-28 05:12:11 -05:00
|
|
|
SecretVersion: &swarmapi.Version{
|
|
|
|
Index: version,
|
|
|
|
},
|
|
|
|
Spec: &secretSpec,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
})
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|