2021-12-06 12:24:22 +00:00
|
|
|
package biz
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/cuigh/auxo/data"
|
|
|
|
"github.com/cuigh/auxo/net/web"
|
|
|
|
"github.com/cuigh/swirl/docker"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SecretBiz interface {
|
2022-01-06 08:54:14 +00:00
|
|
|
Search(ctx context.Context, name string, pageIndex, pageSize int) (secrets []*Secret, total int, err error)
|
|
|
|
Find(ctx context.Context, id string) (secret *Secret, raw string, err error)
|
|
|
|
Delete(ctx context.Context, id, name string, user web.User) (err error)
|
|
|
|
Create(ctx context.Context, secret *Secret, user web.User) (err error)
|
|
|
|
Update(ctx context.Context, secret *Secret, user web.User) (err error)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSecret(d *docker.Docker, eb EventBiz) SecretBiz {
|
|
|
|
return &secretBiz{d: d, eb: eb}
|
|
|
|
}
|
|
|
|
|
|
|
|
type secretBiz struct {
|
|
|
|
d *docker.Docker
|
|
|
|
eb EventBiz
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
func (b *secretBiz) Find(ctx context.Context, id string) (secret *Secret, raw string, err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
var (
|
|
|
|
c swarm.Secret
|
|
|
|
r []byte
|
|
|
|
)
|
2022-01-06 08:54:14 +00:00
|
|
|
c, r, err = b.d.SecretInspect(ctx, id)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil {
|
|
|
|
raw, err = indentJSON(r)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
secret = newSecret(&c)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
func (b *secretBiz) Search(ctx context.Context, name string, pageIndex, pageSize int) ([]*Secret, int, error) {
|
|
|
|
list, total, err := b.d.SecretList(ctx, name, pageIndex, pageSize)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
secrets := make([]*Secret, len(list))
|
|
|
|
for i, n := range list {
|
|
|
|
secrets[i] = newSecret(&n)
|
|
|
|
}
|
|
|
|
return secrets, total, nil
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
func (b *secretBiz) Delete(ctx context.Context, id, name string, user web.User) (err error) {
|
|
|
|
err = b.d.SecretRemove(ctx, id)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil {
|
|
|
|
b.eb.CreateSecret(EventActionDelete, id, name, user)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
func (b *secretBiz) Create(ctx context.Context, c *Secret, user web.User) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
spec := swarm.SecretSpec{
|
|
|
|
Data: []byte(c.Data),
|
|
|
|
}
|
|
|
|
spec.Name = c.Name
|
|
|
|
spec.Labels = toMap(c.Labels)
|
|
|
|
if c.Driver.Name != "" {
|
|
|
|
spec.Driver = &swarm.Driver{
|
|
|
|
Name: c.Driver.Name,
|
|
|
|
Options: toMap(c.Driver.Options),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.Templating.Name != "none" {
|
|
|
|
spec.Templating = &swarm.Driver{
|
|
|
|
Name: c.Templating.Name,
|
|
|
|
Options: toMap(c.Templating.Options),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var id string
|
2022-01-06 08:54:14 +00:00
|
|
|
id, err = b.d.SecretCreate(ctx, &spec)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
b.eb.CreateSecret(EventActionCreate, id, c.Name, user)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
func (b *secretBiz) Update(ctx context.Context, c *Secret, user web.User) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
spec := &swarm.SecretSpec{
|
|
|
|
Data: []byte(c.Data),
|
|
|
|
}
|
|
|
|
spec.Name = c.Name
|
|
|
|
spec.Labels = toMap(c.Labels)
|
|
|
|
if c.Driver.Name != "" {
|
|
|
|
spec.Driver = &swarm.Driver{
|
|
|
|
Name: c.Driver.Name,
|
|
|
|
Options: toMap(c.Driver.Options),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.Templating.Name != "" {
|
|
|
|
spec.Templating = &swarm.Driver{
|
|
|
|
Name: c.Templating.Name,
|
|
|
|
Options: toMap(c.Templating.Options),
|
|
|
|
}
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
err = b.d.SecretUpdate(ctx, c.ID, c.Version, spec)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil {
|
|
|
|
b.eb.CreateSecret(EventActionUpdate, c.ID, c.Name, user)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-12-16 12:23:08 +00:00
|
|
|
|
|
|
|
type Secret struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Version uint64 `json:"version"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
Labels data.Options `json:"labels,omitempty"`
|
|
|
|
Driver Driver `json:"driver"`
|
|
|
|
Templating Driver `json:"templating"`
|
|
|
|
CreatedAt string `json:"createdAt"`
|
|
|
|
UpdatedAt string `json:"updatedAt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSecret(c *swarm.Secret) *Secret {
|
|
|
|
secret := &Secret{
|
|
|
|
ID: c.ID,
|
|
|
|
Name: c.Spec.Name,
|
|
|
|
Version: c.Version.Index,
|
|
|
|
Data: string(c.Spec.Data),
|
|
|
|
Labels: mapToOptions(c.Spec.Labels),
|
|
|
|
CreatedAt: formatTime(c.CreatedAt),
|
|
|
|
UpdatedAt: formatTime(c.UpdatedAt),
|
|
|
|
}
|
|
|
|
if c.Spec.Driver != nil {
|
|
|
|
secret.Driver.Name = c.Spec.Driver.Name
|
|
|
|
secret.Driver.Options = mapToOptions(c.Spec.Driver.Options)
|
|
|
|
}
|
|
|
|
if c.Spec.Templating != nil {
|
|
|
|
secret.Templating.Name = c.Spec.Templating.Name
|
|
|
|
secret.Templating.Options = mapToOptions(c.Spec.Templating.Options)
|
|
|
|
}
|
|
|
|
return secret
|
|
|
|
}
|