2017-10-11 17:21:20 +00:00
|
|
|
package identity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2018-06-07 14:48:36 +00:00
|
|
|
proto "github.com/golang/protobuf/proto"
|
2020-01-06 18:16:52 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-10-11 17:21:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (g *Group) Clone() (*Group, error) {
|
|
|
|
if g == nil {
|
|
|
|
return nil, fmt.Errorf("nil group")
|
|
|
|
}
|
|
|
|
|
|
|
|
marshaledGroup, err := proto.Marshal(g)
|
|
|
|
if err != nil {
|
2021-05-07 12:53:20 +00:00
|
|
|
return nil, fmt.Errorf("failed to marshal group: %w", err)
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var clonedGroup Group
|
|
|
|
err = proto.Unmarshal(marshaledGroup, &clonedGroup)
|
|
|
|
if err != nil {
|
2021-05-07 12:53:20 +00:00
|
|
|
return nil, fmt.Errorf("failed to unmarshal group: %w", err)
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &clonedGroup, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Entity) Clone() (*Entity, error) {
|
|
|
|
if e == nil {
|
|
|
|
return nil, fmt.Errorf("nil entity")
|
|
|
|
}
|
|
|
|
|
|
|
|
marshaledEntity, err := proto.Marshal(e)
|
|
|
|
if err != nil {
|
2021-05-07 12:53:20 +00:00
|
|
|
return nil, fmt.Errorf("failed to marshal entity: %w", err)
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var clonedEntity Entity
|
|
|
|
err = proto.Unmarshal(marshaledEntity, &clonedEntity)
|
|
|
|
if err != nil {
|
2021-05-07 12:53:20 +00:00
|
|
|
return nil, fmt.Errorf("failed to unmarshal entity: %w", err)
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &clonedEntity, nil
|
|
|
|
}
|
|
|
|
|
2021-10-15 19:20:00 +00:00
|
|
|
func (e *Entity) UpsertAlias(alias *Alias) {
|
|
|
|
for i, item := range e.Aliases {
|
|
|
|
if item.ID == alias.ID {
|
|
|
|
e.Aliases[i] = alias
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.Aliases = append(e.Aliases, alias)
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:21:20 +00:00
|
|
|
func (p *Alias) Clone() (*Alias, error) {
|
|
|
|
if p == nil {
|
|
|
|
return nil, fmt.Errorf("nil alias")
|
|
|
|
}
|
|
|
|
|
|
|
|
marshaledAlias, err := proto.Marshal(p)
|
|
|
|
if err != nil {
|
2021-05-07 12:53:20 +00:00
|
|
|
return nil, fmt.Errorf("failed to marshal alias: %w", err)
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var clonedAlias Alias
|
|
|
|
err = proto.Unmarshal(marshaledAlias, &clonedAlias)
|
|
|
|
if err != nil {
|
2021-05-07 12:53:20 +00:00
|
|
|
return nil, fmt.Errorf("failed to unmarshal alias: %w", err)
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &clonedAlias, nil
|
|
|
|
}
|
2020-01-06 18:16:52 +00:00
|
|
|
|
|
|
|
// ToSDKAlias converts the provided alias to an SDK compatible alias.
|
|
|
|
func ToSDKAlias(a *Alias) *logical.Alias {
|
|
|
|
if a == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
metadata := make(map[string]string, len(a.Metadata))
|
|
|
|
for k, v := range a.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Alias{
|
2021-09-17 18:03:47 +00:00
|
|
|
Name: a.Name,
|
|
|
|
ID: a.ID,
|
|
|
|
MountAccessor: a.MountAccessor,
|
|
|
|
MountType: a.MountType,
|
|
|
|
Metadata: metadata,
|
|
|
|
NamespaceID: a.NamespaceID,
|
|
|
|
CustomMetadata: a.CustomMetadata,
|
2020-01-06 18:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSDKEntity converts the provided entity to an SDK compatible entity.
|
|
|
|
func ToSDKEntity(e *Entity) *logical.Entity {
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
aliases := make([]*logical.Alias, len(e.Aliases))
|
|
|
|
|
|
|
|
for i, a := range e.Aliases {
|
|
|
|
aliases[i] = ToSDKAlias(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata := make(map[string]string, len(e.Metadata))
|
|
|
|
for k, v := range e.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Entity{
|
|
|
|
ID: e.ID,
|
|
|
|
Name: e.Name,
|
|
|
|
Disabled: e.Disabled,
|
|
|
|
Aliases: aliases,
|
|
|
|
Metadata: metadata,
|
|
|
|
NamespaceID: e.NamespaceID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSDKGroup converts the provided group to an SDK compatible group.
|
|
|
|
func ToSDKGroup(g *Group) *logical.Group {
|
|
|
|
if g == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata := make(map[string]string, len(g.Metadata))
|
|
|
|
for k, v := range g.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Group{
|
|
|
|
ID: g.ID,
|
|
|
|
Name: g.Name,
|
|
|
|
Metadata: metadata,
|
|
|
|
NamespaceID: g.NamespaceID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSDKGroups converts the provided group list to an SDK compatible group list.
|
|
|
|
func ToSDKGroups(groups []*Group) []*logical.Group {
|
|
|
|
if groups == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := make([]*logical.Group, len(groups))
|
|
|
|
|
|
|
|
for i, g := range groups {
|
|
|
|
ret[i] = ToSDKGroup(g)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|