-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathuser_support.go
More file actions
232 lines (186 loc) · 6.3 KB
/
user_support.go
File metadata and controls
232 lines (186 loc) · 6.3 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// This file is part of MinIO Console Server
// Copyright (c) 2023 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package api
import (
"errors"
"fmt"
"github.com/go-openapi/runtime/middleware"
"github.com/minio/console/api/operations"
"github.com/minio/console/api/operations/support"
"github.com/minio/console/models"
"github.com/minio/console/pkg/subnet"
"golang.org/x/net/context"
)
type ConfigurationSetItem struct {
Value string
Enable bool
}
func registerSupportHandlers(api *operations.ConsoleAPI) {
// callhome handlers
api.SupportGetCallHomeOptionValueHandler = support.GetCallHomeOptionValueHandlerFunc(func(params support.GetCallHomeOptionValueParams, session *models.Principal) middleware.Responder {
callhomeResp, err := getCallHomeOptionResponse(session, params)
if err != nil {
return support.NewGetCallHomeOptionValueDefault(err.Code).WithPayload(err.APIError)
}
return support.NewGetCallHomeOptionValueOK().WithPayload(callhomeResp)
})
api.SupportSetCallHomeStatusHandler = support.SetCallHomeStatusHandlerFunc(func(params support.SetCallHomeStatusParams, session *models.Principal) middleware.Responder {
err := editCallHomeOptionResponse(session, params)
if err != nil {
return support.NewSetCallHomeStatusDefault(err.Code).WithPayload(err.APIError)
}
return support.NewSetCallHomeStatusNoContent()
})
}
// getCallHomeOptionResponse returns the selected option value
func getCallHomeOptionResponse(session *models.Principal, params support.GetCallHomeOptionValueParams) (*models.CallHomeGetResponse, *CodedAPIError) {
ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
defer cancel()
mAdmin, err := NewMinioAdminClient(params.HTTPRequest.Context(), session)
if err != nil {
return nil, ErrorWithContext(ctx, err)
}
minioClient := AdminClient{Client: mAdmin}
response, err := getCallHomeRule(ctx, minioClient)
if err != nil {
return nil, ErrorWithContext(ctx, err)
}
return response, nil
}
func getCallHomeRule(ctx context.Context, client MinioAdmin) (*models.CallHomeGetResponse, error) {
// We verify if callhome SubSys is supported
supportedSubSys, err := minioConfigSupportsSubSys(ctx, client, "callhome")
if err != nil {
return nil, err
}
var returnResponse models.CallHomeGetResponse
// SubSys is not supported, hence callhome is disabled.
if !supportedSubSys {
returnResponse.DiagnosticsStatus = false
returnResponse.LogsStatus = false
return &returnResponse, nil
}
diagnosticsProps, err := getConfig(ctx, client, "callhome")
if err != nil {
return nil, err
}
diagnosticsSt := true
for _, properties := range diagnosticsProps {
for _, property := range properties.KeyValues {
if property.Key == "enable" {
diagnosticsSt = property.Value == "on"
}
}
}
loggerSt := true
loggerProps, err := getConfig(ctx, client, "logger_webhook:subnet")
// Logger not defined, then it is disabled.
if err != nil {
loggerSt = false
} else {
for _, logger := range loggerProps {
for _, property := range logger.KeyValues {
if property.Key == "enable" {
loggerSt = property.Value == "on"
}
}
}
}
returnModel := models.CallHomeGetResponse{DiagnosticsStatus: diagnosticsSt, LogsStatus: loggerSt}
return &returnModel, nil
}
// editCallHomeOptionResponse returns if there was an error setting the option
func editCallHomeOptionResponse(session *models.Principal, params support.SetCallHomeStatusParams) *CodedAPIError {
ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
defer cancel()
mAdmin, err := NewMinioAdminClient(params.HTTPRequest.Context(), session)
if err != nil {
return ErrorWithContext(ctx, err)
}
minioClient := AdminClient{Client: mAdmin}
err = setCallHomeConfiguration(ctx, minioClient, *params.Body.DiagState, *params.Body.LogsState)
if err != nil {
return ErrorWithContext(ctx, err)
}
return nil
}
func configureCallHomeDiagnostics(ctx context.Context, client MinioAdmin, diagState bool) error {
// We verify if callhome SubSys is supported
supportedSubSys, err := minioConfigSupportsSubSys(ctx, client, "callhome")
if err != nil {
return err
}
// SubSys is not supported, hence callhome not available
if !supportedSubSys {
return errors.New("your version of MinIO doesn't support this configuration")
}
enableStr := "off"
if diagState {
enableStr = "on"
}
configStr := "callhome enable=" + enableStr
_, err = client.setConfigKV(ctx, configStr)
if err != nil {
return err
}
return nil
}
func configureCallHomeLogs(ctx context.Context, client MinioAdmin, logState bool, apiKey string) error {
var configStr string
if logState {
configStr = fmt.Sprintf("logger_webhook:subnet endpoint=%s auth_token=%s enable=on",
subnet.LogWebhookURL(), apiKey)
} else {
configStr = "logger_webhook:subnet enable=off"
}
// Call set config API
_, err := client.setConfigKV(ctx, configStr)
if err != nil {
return err
}
return nil
}
func setCallHomeConfiguration(ctx context.Context, client MinioAdmin, diagState, logsState bool) error {
tokenConfig, err := GetSubnetKeyFromMinIOConfig(ctx, client)
if err != nil {
return err
}
apiKey := tokenConfig.APIKey
if len(apiKey) == 0 {
return errors.New("please register this cluster in subnet to continue")
}
err = configureCallHomeDiagnostics(ctx, client, diagState)
if err != nil {
return err
}
err = configureCallHomeLogs(ctx, client, logsState, apiKey)
if err != nil {
return err
}
return nil
}
func minioConfigSupportsSubSys(ctx context.Context, client MinioAdmin, subSys string) (bool, error) {
help, err := client.helpConfigKVGlobal(ctx, false)
if err != nil {
return false, err
}
for _, h := range help.KeysHelp {
if h.Key == subSys {
return true, nil
}
}
return false, nil
}