This repository was archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
185 lines (161 loc) · 5 KB
/
plugin.go
File metadata and controls
185 lines (161 loc) · 5 KB
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
181
182
183
184
185
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
"code.cloudfoundry.org/cli/plugin"
"code.cloudfoundry.org/cli/plugin/models"
)
// TLSEnablerPlugin allows you to quickly enable TLS for selected service instances
type TLSEnablerPlugin struct {
cliConnection plugin.CliConnection
serviceName string
}
// maps supported service type to the arbitrary parameter name
var supportedServices = map[string]string{
"p.rabbitmq": "tls",
"p.mysql": "enable_tls",
"rabbitmq-odb-bosh-lite": "tls",
}
// Run is the main entry point for CF CLI plugins
func (t *TLSEnablerPlugin) Run(cliConnection plugin.CliConnection, args []string) {
t.cliConnection = cliConnection
switch args[0] {
case "CLI-MESSAGE-UNINSTALL":
return
case "enable-tls":
if len(args) != 2 {
log.Fatalln("USAGE: cf enable-tls SERVICE_NAME")
}
t.serviceName = args[1]
err := t.enableTLS()
if err != nil {
log.Fatalf("Failed to enable TLS: %v", err)
}
case "create-service-with-tls":
if len(args) < 4 {
log.Fatalln("USAGE: create-service-with-tls SERVICE PLAN SERVICE_INSTANCE [-c PARAMETERS_AS_JSON] [-t TAGS]")
}
args[0] = "create-service"
t.serviceName = args[3]
err := t.createServiceWithTLS(args)
if err != nil {
log.Fatalf("Failed to create service with TLS: %v", err)
}
default:
log.Fatalf("Received unexpected command %v\n", args[0])
}
}
// GetMetadata returns plugin information
func (t *TLSEnablerPlugin) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "TLSEnabler",
Version: plugin.VersionType{
Major: 0,
Minor: 2,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "enable-tls",
Alias: "",
HelpText: "Enables TLS on the specified service instance",
UsageDetails: plugin.Usage{
Usage: "enable-tls NAME - enable TLS on service instance NAME",
},
},
{
Name: "create-service-with-tls",
Alias: "",
HelpText: "executes create-service and then immediately enable-tls",
UsageDetails: plugin.Usage{
Usage: "create-service-with-tls SERVICE PLAN SERVICE_INSTANCE [-c PARAMETERS_AS_JSON] [-t TAGS]",
},
},
},
}
}
func (t *TLSEnablerPlugin) enableTLS() error {
log.Printf("Enabling TLS on service %v\n", t.serviceName)
serviceInfo, err := t.cliConnection.GetService(t.serviceName)
if err != nil {
return err
}
if _, ok := supportedServices[serviceInfo.ServiceOffering.Name]; !ok {
log.Fatalf("Sorry, I don't know how to enable TLS on an instance of %v service\n", serviceInfo.ServiceOffering.Name)
}
arbitraryParameters, err := t.buildArbitraryParameters(serviceInfo)
if err != nil {
return err
}
_, err = t.cliConnection.CliCommand("update-service", t.serviceName, "-c", arbitraryParameters)
if err != nil {
return err
}
return nil
}
func (t *TLSEnablerPlugin) createServiceWithTLS(args []string) error {
_, err := t.cliConnection.CliCommand(args...)
if err != nil {
return err
}
// wait for `create` to complete
t.waitUntilServiceCreated()
return t.enableTLS()
}
func (t *TLSEnablerPlugin) waitUntilServiceCreated() error {
for {
service, err := t.cliConnection.GetService(t.serviceName)
if err != nil {
return err
}
if service.LastOperation.State == "succeeded" {
return nil
} else if service.LastOperation.State == "failed" {
return fmt.Errorf(
"error %s [status: %s]",
service.LastOperation.Description,
service.LastOperation.State,
)
}
time.Sleep(500 * time.Millisecond)
}
}
func (t *TLSEnablerPlugin) buildArbitraryParameters(serviceInfo plugin_models.GetService_Model) (string, error) {
serviceKeyName := "temporary-key-to-enable-tls"
_, err := t.cliConnection.CliCommand("create-service-key", serviceInfo.Name, serviceKeyName)
if err != nil {
return "", err
}
serviceKey, err := t.getServiceKey(serviceKeyName)
if err != nil {
return "", err
}
// ideally it should be used with defer() but it doesn't work (gets triggered but the key doesn't get deleted)
t.cliConnection.CliCommand("delete-service-key", "-f", serviceInfo.Name, serviceKeyName)
hostnames := t.getHostnamesFromServiceKey(serviceKey)
return fmt.Sprintf("{\"%v\": [%v]}", supportedServices[serviceInfo.ServiceOffering.Name], strings.Join(hostnames, ",")), nil
}
func (t *TLSEnablerPlugin) getServiceKey(serviceKeyName string) (map[string]interface{}, error) {
output, err := t.cliConnection.CliCommand("service-key", t.serviceName, serviceKeyName)
if err != nil {
log.Fatal(err)
}
var serviceKey map[string]interface{}
json.Unmarshal([]byte(strings.Join(output[2:], "")), &serviceKey)
return serviceKey, nil
}
func (t *TLSEnablerPlugin) getHostnamesFromServiceKey(serviceKey map[string]interface{}) []string {
var hs []string
if hostnames, ok := serviceKey["hostnames"]; ok {
for _, h := range hostnames.([]interface{}) {
hs = append(hs, fmt.Sprintf("\"%v\"", h.(string)))
}
return hs
}
// this is a single-node service which doesn't reutrn `hostnames`
hs = []string{fmt.Sprintf("\"%v\"", serviceKey["hostname"].(string))}
return hs
}