/
userprojectbinding_openstack.go
158 lines (129 loc) · 4.29 KB
/
userprojectbinding_openstack.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
package controllers
import (
"context"
"fmt"
"net/url"
"strings"
"github.com/go-logr/logr"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/identity/v3/applicationcredentials"
"github.com/pluscloudopen/reseller-cli/v2/pkg/openapi"
"github.com/pluscloudopen/reseller-operator/api/v1alpha1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
const applicationCredentialIdKey = "application-credential-id"
const applicationCredentialSecretKey = "application-credential-secret"
func (r *UserProjectBindingReconciler) ensureApplicationCredential(ctx context.Context, logger logr.Logger, upb *v1alpha1.UserProjectBinding, region v1alpha1.Region, project openapi.ProjectCreatedResponse, userId string, username string, password string) error {
svc, err := openStackIdentityClient(region, project, username, password)
if err != nil {
return err
}
appCredName := getApplicationCredentialName(project.Name, username)
pager := applicationcredentials.List(svc, userId, applicationcredentials.ListOpts{Name: appCredName})
if pager.Err != nil {
return pager.Err
}
pages, err := pager.AllPages()
if err != nil {
return err
}
isEmpty, err := pages.IsEmpty()
if err != nil {
return err
}
//Create application credential if it doesn't exist
if isEmpty {
result := applicationcredentials.Create(svc, userId, applicationcredentials.CreateOpts{
Name: appCredName,
Description: "Generated by PCO Reseller Operator",
})
if result.Err != nil {
return result.Err
}
ac, err := result.Extract()
if err != nil {
return err
}
isTrue := true
accessSecret := &v1.Secret{
ObjectMeta: meta_v1.ObjectMeta{
Namespace: upb.Namespace,
Name: upb.ApplicationCredentialName(),
},
Immutable: &isTrue,
StringData: map[string]string{
applicationCredentialIdKey: ac.ID,
applicationCredentialSecretKey: ac.Secret,
},
}
if err := r.createOrPatch(ctx, accessSecret); err != nil {
return err
}
logger.Info(fmt.Sprintf("Application credential %s created", appCredName))
} else {
logger.Info(fmt.Sprintf("Application credential %s already exists", appCredName))
}
return nil
}
func (r *UserProjectBindingReconciler) deprovisionApplicationCredential(ctx context.Context, logger logr.Logger, upb *v1alpha1.UserProjectBinding, region v1alpha1.Region, project openapi.ProjectCreatedResponse, userId string, username string, password string) error {
accessSecretName := types.NamespacedName{
Name: upb.ApplicationCredentialName(),
Namespace: upb.Namespace,
}
accessSecret := &v1.Secret{}
if err := r.Get(ctx, accessSecretName, accessSecret); err != nil {
if !errors.IsNotFound(err) {
return err
}
logger.Info("Application Credential already gone")
return nil
}
svc, err := openStackIdentityClient(region, project, username, password)
if err != nil {
return nil
}
result := applicationcredentials.Delete(svc, userId, string(accessSecret.Data[applicationCredentialIdKey]))
if result.Err != nil {
if !strings.Contains(result.Err.Error(), "Resource not found") {
return result.Err
}
}
if err := r.Delete(ctx, accessSecret); err != nil {
if !errors.IsNotFound(err) {
return err
}
}
logger.Info("Application Credential deleted")
return nil
}
func openStackIdentityClient(region v1alpha1.Region, project openapi.ProjectCreatedResponse, username string, password string) (*gophercloud.ServiceClient, error) {
url, err := url.Parse(region.Spec.Endpoint)
if err != nil {
return nil, err
}
keyStoneUrl := fmt.Sprintf("https://%s:5000", strings.Split(url.Host, ":")[0])
domainName := strings.Split(project.Name, "-")[0]
opts := gophercloud.AuthOptions{
IdentityEndpoint: keyStoneUrl,
Username: username,
Password: password,
TenantName: project.Name,
DomainName: domainName,
}
client, err := openstack.AuthenticatedClient(opts)
if err != nil {
return nil, err
}
svc, err := openstack.NewIdentityV3(client, gophercloud.EndpointOpts{})
if err != nil {
return nil, err
}
return svc, nil
}
func getApplicationCredentialName(projectName string, username string) string {
return fmt.Sprintf("%s-%s", projectName, username)
}