-
Notifications
You must be signed in to change notification settings - Fork 491
/
client.go
180 lines (162 loc) · 5.83 KB
/
client.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2023 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package secretbackends
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/juju/api/base"
apiservererrors "github.com/juju/juju/apiserver/errors"
coresecrets "github.com/juju/juju/core/secrets"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/secrets"
"github.com/juju/juju/secrets/provider"
)
// Client is the api client for accessing secret backend related facades.
type Client struct {
facade base.FacadeCaller
}
// NewClient creates a secrets api client.
func NewClient(facade base.FacadeCaller) *Client {
return &Client{facade: facade}
}
// GetSecretBackendConfig fetches the config needed to make a secret backend client.
// If backendID is nil, fetch the current active backend config.
func (c *Client) GetSecretBackendConfig(backendID *string) (*provider.ModelBackendConfigInfo, error) {
var results params.SecretBackendConfigResults
args := params.SecretBackendArgs{}
if backendID != nil {
args.BackendIDs = []string{*backendID}
}
err := c.facade.FacadeCall("GetSecretBackendConfigs", args, &results)
if err != nil && !errors.Is(err, errors.NotFound) {
return nil, errors.Trace(err)
}
if err != nil || len(results.Results) == 0 {
msg := "active secret backend"
if backendID != nil {
msg = fmt.Sprintf("external secret backend id %q", *backendID)
}
return nil, errors.NotFoundf(msg)
}
if len(results.Results) != 1 {
return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
}
info := &provider.ModelBackendConfigInfo{
ActiveID: results.ActiveID,
Configs: make(map[string]provider.ModelBackendConfig),
}
for id, cfg := range results.Results {
info.Configs[id] = provider.ModelBackendConfig{
ControllerUUID: cfg.ControllerUUID,
ModelUUID: cfg.ModelUUID,
ModelName: cfg.ModelName,
BackendConfig: provider.BackendConfig{
BackendType: cfg.Config.BackendType,
Config: cfg.Config.Params,
},
}
}
return info, nil
}
// GetBackendConfigForDrain fetches the config needed to make a secret backend client for the drain worker.
func (c *Client) GetBackendConfigForDrain(backendID *string) (*provider.ModelBackendConfig, string, error) {
var result params.SecretBackendConfigResults
arg := params.SecretBackendArgs{ForDrain: true}
if backendID != nil {
arg.BackendIDs = []string{*backendID}
}
err := c.facade.FacadeCall("GetSecretBackendConfigs", arg, &result)
if err != nil && !errors.Is(err, errors.NotFound) {
return nil, "", errors.Trace(err)
}
if len(result.Results) == 0 {
return nil, "", errors.NotFoundf("no secret backends available")
}
for _, cfg := range result.Results {
return &provider.ModelBackendConfig{
ControllerUUID: cfg.ControllerUUID,
ModelUUID: cfg.ModelUUID,
ModelName: cfg.ModelName,
BackendConfig: provider.BackendConfig{
BackendType: cfg.Config.BackendType,
Config: cfg.Config.Params,
},
}, result.ActiveID, nil
}
if backendID != nil {
return nil, "", errors.NotFoundf("secret backend %q", *backendID)
}
return nil, "", errors.NotFoundf("active secret backend")
}
// GetContentInfo returns info about the content of a secret.
func (c *Client) GetContentInfo(uri *coresecrets.URI, label string, refresh, peek bool) (*secrets.ContentParams, *provider.ModelBackendConfig, bool, error) {
arg := params.GetSecretContentArg{
Label: label,
Refresh: refresh,
Peek: peek,
}
if uri != nil {
arg.URI = uri.String()
}
var results params.SecretContentResults
if err := c.facade.FacadeCall(
"GetSecretContentInfo", params.GetSecretContentArgs{Args: []params.GetSecretContentArg{arg}}, &results,
); err != nil {
return nil, nil, false, errors.Trace(err)
}
return c.processSecretContentResults(results)
}
func (c *Client) processSecretContentResults(results params.SecretContentResults) (*secrets.ContentParams, *provider.ModelBackendConfig, bool, error) {
if n := len(results.Results); n != 1 {
return nil, nil, false, errors.Errorf("expected 1 result, got %d", n)
}
if err := results.Results[0].Error; err != nil {
return nil, nil, false, apiservererrors.RestoreError(err)
}
content := &secrets.ContentParams{}
var (
backendConfig *provider.ModelBackendConfig
draining bool
)
result := results.Results[0]
contentParams := results.Results[0].Content
if contentParams.ValueRef != nil {
content.ValueRef = &coresecrets.ValueRef{
BackendID: contentParams.ValueRef.BackendID,
RevisionID: contentParams.ValueRef.RevisionID,
}
if result.BackendConfig == nil {
return nil, nil, false, errors.Errorf("missing secret backend info for %q", content.ValueRef)
}
backendConfig = &provider.ModelBackendConfig{
ControllerUUID: result.BackendConfig.ControllerUUID,
ModelUUID: result.BackendConfig.ModelUUID,
ModelName: result.BackendConfig.ModelName,
BackendConfig: provider.BackendConfig{
BackendType: result.BackendConfig.Config.BackendType,
Config: result.BackendConfig.Config.Params,
},
}
draining = result.BackendConfig.Draining
}
if len(contentParams.Data) > 0 {
content.SecretValue = coresecrets.NewSecretValue(contentParams.Data)
}
return content, backendConfig, draining, nil
}
// GetRevisionContentInfo returns info about the content of a secret revision.
// If pendingDelete is true, the revision is marked for deletion.
func (c *Client) GetRevisionContentInfo(uri *coresecrets.URI, revision int, pendingDelete bool) (*secrets.ContentParams, *provider.ModelBackendConfig, bool, error) {
arg := params.SecretRevisionArg{
URI: uri.String(),
Revisions: []int{revision},
PendingDelete: pendingDelete,
}
var results params.SecretContentResults
if err := c.facade.FacadeCall(
"GetSecretRevisionContentInfo", arg, &results,
); err != nil {
return nil, nil, false, errors.Trace(err)
}
return c.processSecretContentResults(results)
}