-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapigee_token.go
146 lines (120 loc) · 3.49 KB
/
apigee_token.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
package secretsengine
import (
"context"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
apigeeSecretType = "apigee_secret"
)
type apigeeToken struct {
OrgName string `json:"org_name"`
DeveloperEmail string `json:"developer_email"`
AppName string `json:"app_name"`
ApiProducts string `json:"api_products"`
Key string `json:"key"`
Secret string `json:"secret"`
Credentials string `json:"credentials"`
}
func (b *apigeeBackend) apigeeToken() *framework.Secret {
return &framework.Secret{
Type: apigeeSecretType,
Fields: map[string]*framework.FieldSchema{
"org_name": {
Type: framework.TypeString,
Description: "Apigee OrgName",
},
"developer_email": {
Type: framework.TypeString,
Description: "Apigee DeveloperEmail",
},
"app_name": {
Type: framework.TypeString,
Description: "Apigee AppName",
},
"api_products": {
Type: framework.TypeString,
Description: "Apigee ApiProducts",
},
"key": {
Type: framework.TypeString,
Description: "Apigee Key",
},
"secret": {
Type: framework.TypeString,
Description: "Apigee Secret",
},
"credentials": {
Type: framework.TypeString,
Description: "Apigee Credentials",
},
},
Revoke: b.credentialsRevoke,
}
}
func (b *apigeeBackend) credentialsRevoke(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
client, err := b.getClient(ctx, req.Storage)
if err != nil {
return nil, fmt.Errorf("error getting client: %w", err)
}
orgName := ""
developerEmail := ""
appName := ""
key := ""
orgNameRaw, ok := req.Secret.InternalData["org_name"]
if ok {
orgName, ok = orgNameRaw.(string)
if !ok {
return nil, fmt.Errorf("invalid value for org_name in secret internal data")
}
}
developerEmailRaw, ok := req.Secret.InternalData["developer_email"]
if ok {
developerEmail, ok = developerEmailRaw.(string)
if !ok {
return nil, fmt.Errorf("invalid value for developer_email in secret internal data")
}
}
appNameRaw, ok := req.Secret.InternalData["app_name"]
if ok {
appName, ok = appNameRaw.(string)
if !ok {
return nil, fmt.Errorf("invalid value for app_name in secret internal data")
}
}
keyRaw, ok := req.Secret.InternalData["key"]
if ok {
key, ok = keyRaw.(string)
if !ok {
return nil, fmt.Errorf("invalid value for key in secret internal data")
}
}
err = deleteCredentials(ctx, client, orgName, developerEmail, appName, key)
if err != nil {
return nil, fmt.Errorf("error deleting credentials: %w", err)
}
return nil, nil
}
func createCredentials(ctx context.Context, c *apigeeClient, orgName string, developerEmail string, appName string, apiProducts string, ttl int) (*apigeeToken, error) {
response, err := c.CreateCredentials(orgName, developerEmail, appName, apiProducts, ttl)
if err != nil {
return nil, fmt.Errorf("error creating credentials: %w", err)
}
return &apigeeToken{
OrgName: orgName,
DeveloperEmail: developerEmail,
AppName: appName,
ApiProducts: apiProducts,
Key: response.Key,
Secret: response.Secret,
Credentials: response.Credentials,
}, nil
}
func deleteCredentials(ctx context.Context, c *apigeeClient, orgName string, developerEmail string, appName string, key string) error {
err := c.DeleteCredentials(orgName, developerEmail, appName, key)
if err != nil {
return err
}
return nil
}