-
Notifications
You must be signed in to change notification settings - Fork 7
/
dynamic_role.go
66 lines (55 loc) · 1.87 KB
/
dynamic_role.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package openldap
import (
"context"
"fmt"
"path"
"time"
"github.com/hashicorp/vault/sdk/logical"
)
type dynamicRole struct {
// required fields
Name string `json:"name" mapstructure:"name"`
CreationLDIF string `json:"creation_ldif" mapstructure:"creation_ldif"`
DeletionLDIF string `json:"deletion_ldif" mapstructure:"deletion_ldif"`
// optional fields
RollbackLDIF string `json:"rollback_ldif" mapstructure:"rollback_ldif,omitempty"`
UsernameTemplate string `json:"username_template,omitempty" mapstructure:"username_template,omitempty"`
DefaultTTL time.Duration `json:"default_ttl,omitempty" mapstructure:"default_ttl,omitempty"`
MaxTTL time.Duration `json:"max_ttl,omitempty" mapstructure:"max_ttl,omitempty"`
}
func retrieveDynamicRole(ctx context.Context, s logical.Storage, roleName string) (*dynamicRole, error) {
entry, err := s.Get(ctx, path.Join(dynamicRolePath, roleName))
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
result := new(dynamicRole)
if err := entry.DecodeJSON(result); err != nil {
return nil, err
}
return result, nil
}
func storeDynamicRole(ctx context.Context, s logical.Storage, role *dynamicRole) error {
if role.Name == "" {
return fmt.Errorf("missing role name")
}
entry, err := logical.StorageEntryJSON(path.Join(dynamicRolePath, role.Name), role)
if err != nil {
return fmt.Errorf("unable to marshal storage entry: %w", err)
}
err = s.Put(ctx, entry)
if err != nil {
return fmt.Errorf("failed to store dynamic role: %w", err)
}
return nil
}
func deleteDynamicRole(ctx context.Context, s logical.Storage, roleName string) error {
if roleName == "" {
return fmt.Errorf("missing role name")
}
return s.Delete(ctx, path.Join(dynamicRolePath, roleName))
}