-
Notifications
You must be signed in to change notification settings - Fork 56
/
addplugin.go
318 lines (292 loc) · 13.8 KB
/
addplugin.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//(C) Copyright [2020] Hewlett Packard Enterprise Development LP
//
//Licensed under the Apache License, Version 2.0 (the "License"); you may
//not use this file except in compliance with the License. You may obtain
//a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
//License for the specific language governing permissions and limitations
// under the License.
package system
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/ODIM-Project/ODIM/lib-dmtf/model"
"github.com/ODIM-Project/ODIM/lib-utilities/common"
"github.com/ODIM-Project/ODIM/lib-utilities/config"
"github.com/ODIM-Project/ODIM/lib-utilities/errors"
l "github.com/ODIM-Project/ODIM/lib-utilities/logs"
"github.com/ODIM-Project/ODIM/lib-utilities/response"
"github.com/ODIM-Project/ODIM/svc-aggregation/agcommon"
"github.com/ODIM-Project/ODIM/svc-aggregation/agmodel"
"github.com/ODIM-Project/ODIM/svc-aggregation/agresponse"
)
func (e *ExternalInterface) addPluginData(ctx context.Context, req AddResourceRequest, taskID, targetURI string, pluginContactRequest getResourceRequest, queueList []string, cmVariants connectionMethodVariants) (response.RPC, string, []byte) {
var resp response.RPC
taskInfo := &common.TaskUpdateInfo{Context: ctx, TaskID: taskID, TargetURI: targetURI, UpdateTask: e.UpdateTask, TaskRequest: pluginContactRequest.TaskRequest}
if !(cmVariants.PreferredAuthType == "BasicAuth" || cmVariants.PreferredAuthType == "XAuthToken") {
errMsg := "error: incorrect request property value for PreferredAuthType"
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusBadRequest, response.PropertyValueNotInList, errMsg, []interface{}{"PreferredAuthType", "[BasicAuth, XAuthToken]"}, taskInfo), "", nil
}
// checking the plugin type
if !isPluginTypeSupported(cmVariants.PluginType) {
errMsg := "error: incorrect request property value for PluginType"
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusBadRequest, response.PropertyValueNotInList, errMsg, []interface{}{"PluginType", fmt.Sprintf("%v", config.Data.SupportedPluginTypes)}, taskInfo), "", nil
}
// checking whether the Plugin already exists
// If GetPluginData was successful, it indicates plugin already exists,
// but it could also return errors, for below reasons, and has to be considered
// as successful fetch of plugin data
// error is nil - Plugin was successfully fetched.
// error is not nil, Plugin data read but JSON unmarshaling failed
// error is not nil, Plugin data read but decryption of plugin password failed
// error is not nil, DB query failed, can't say for sure if queried plugin exists,
// except when read fails with plugin data not found, and will continue with add process,
// and any other errors, will fail add plugin operation.
_, errs := agmodel.GetPluginData(cmVariants.PluginID)
if errs == nil || (errs != nil && (errs.ErrNo() == errors.JSONUnmarshalFailed || errs.ErrNo() == errors.DecryptionFailed)) {
errMsg := "error:plugin with name " + cmVariants.PluginID + " already exists"
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusConflict, response.ResourceAlreadyExists, errMsg, []interface{}{"Plugin", "PluginID", cmVariants.PluginID}, taskInfo), "", nil
}
if errs != nil && errs.ErrNo() != errors.DBKeyNotFound {
errMsg := "error: DB lookup failed for " + cmVariants.PluginID + " plugin: " + errs.Error()
l.LogWithFields(ctx).Error(errMsg)
if errs.ErrNo() == errors.DBConnFailed {
return common.GeneralError(http.StatusServiceUnavailable, response.CouldNotEstablishConnection, errMsg,
[]interface{}{"Backend", config.Data.DBConf.OnDiskHost + ":" + config.Data.DBConf.OnDiskPort}, taskInfo), "", nil
}
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, []interface{}{}, taskInfo), "", nil
}
pluginNameArray, err := agmodel.GetAllKeysFromTable("Plugin")
if err == nil {
for _, ID := range pluginNameArray {
plugin, err := e.GetPluginMgrAddr(ID)
if err != nil && err.ErrNo() == errors.JSONUnmarshalFailed {
continue
}
if err != nil {
return common.GeneralError(http.StatusServiceUnavailable, response.CouldNotEstablishConnection, err.Error(),
[]interface{}{"Backend", config.Data.DBConf.OnDiskHost + ":" + config.Data.DBConf.OnDiskPort}, taskInfo), "", nil
}
if plugin.IP+":"+plugin.Port == req.ManagerAddress {
errMsg := "error:plugin with manager adress " + req.ManagerAddress + " already exists with name " + plugin.ID + " and ManagerUUID " + plugin.ManagerUUID
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusConflict, response.ResourceAlreadyExists, errMsg, []interface{}{"Plugin", "PluginID", ID}, taskInfo), "", nil
}
}
} else {
return common.GeneralError(http.StatusServiceUnavailable, response.CouldNotEstablishConnection, err.Error(),
[]interface{}{"Backend", config.Data.DBConf.OnDiskHost + ":" + config.Data.DBConf.OnDiskPort}, taskInfo), "", nil
}
// encrypt plugin password
ciphertext, err := e.EncryptPassword([]byte(req.Password))
if err != nil {
errMsg := "error: encryption failed: " + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, taskInfo), "", nil
}
var managerUUID string
ipData := strings.Split(req.ManagerAddress, ":")
var plugin = agmodel.Plugin{
IP: ipData[0],
Port: ipData[1],
Username: req.UserName,
Password: []byte(req.Password),
ID: cmVariants.PluginID,
PluginType: cmVariants.PluginType,
PreferredAuthType: cmVariants.PreferredAuthType,
}
pluginContactRequest.Plugin = plugin
pluginContactRequest.StatusPoll = true
if strings.EqualFold(plugin.PreferredAuthType, "XAuthToken") {
pluginContactRequest.HTTPMethodType = http.MethodPost
pluginContactRequest.DeviceInfo = map[string]interface{}{
"Username": plugin.Username,
"Password": string(plugin.Password),
}
pluginContactRequest.OID = "/ODIM/v1/Sessions"
_, token, getResponse, err := contactPlugin(ctx, pluginContactRequest, "error while creating the session: ")
if err != nil {
errMsg := err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, errMsg, getResponse.MsgArgs, taskInfo), "", nil
}
pluginContactRequest.Token = token
} else {
pluginContactRequest.LoginCredentials = map[string]string{
"UserName": plugin.Username,
"Password": string(plugin.Password),
}
}
// Getting all managers info from plugin
pluginContactRequest.HTTPMethodType = http.MethodGet
pluginContactRequest.OID = "/ODIM/v1/Managers"
body, _, getResponse, err := contactPlugin(ctx, pluginContactRequest, "error while getting the details "+pluginContactRequest.OID+": ")
if err != nil {
errMsg := err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, errMsg, getResponse.MsgArgs, taskInfo), "", nil
}
// Extract all managers info and loop over each members
managersMap := make(map[string]interface{})
err = json.Unmarshal([]byte(body), &managersMap)
if err != nil {
errMsg := "unable to parse the managers resposne" + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, taskInfo), "", nil
}
var managersData = make(map[string][]byte)
managerMembers := managersMap["Members"]
// Getting the indivitual managers response
for _, object := range managerMembers.([]interface{}) {
pluginContactRequest.OID = object.(map[string]interface{})["@odata.id"].(string)
body, _, getResponse, err := contactPlugin(ctx, pluginContactRequest, "error while getting the details "+pluginContactRequest.OID+": ")
if err != nil {
errMsg := err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, errMsg, getResponse.MsgArgs, taskInfo), "", nil
}
managerData := make(map[string]interface{})
err = json.Unmarshal([]byte(body), &managerData)
if err != nil {
errMsg := "unable to parse the managers resposne" + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, taskInfo), "", nil
}
if uuid, ok := managerData["UUID"]; ok {
managerUUID = uuid.(string)
}
managersData[pluginContactRequest.OID] = body
}
//adding empty logservices collection
ldata := model.Collection{
ODataContext: "/redfish/v1/$metadata#LogServiceCollection.LogServiceCollection",
ODataID: "/redfish/v1/Managers/" + managerUUID + "/LogServices",
ODataType: "#LogServiceCollection.LogServiceCollection",
Description: "Logs view",
Members: []*model.Link{
&model.Link{
Oid: "/redfish/v1/Managers/" + managerUUID + "/LogServices/SL",
},
},
MembersCount: 1,
Name: "Logs",
}
dbdata, err := json.Marshal(ldata)
if err != nil {
errMsg := "unable to marshal manager data: %v" + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, taskInfo), "", nil
}
key := "/redfish/v1/Managers/" + managerUUID + "/LogServices"
dbEr := agmodel.SavePluginManagerInfo([]byte(dbdata), "LogServicesCollection", key)
if dbEr != nil {
errMsg := dbEr.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusConflict, response.ResourceAlreadyExists, errMsg, []interface{}{"Plugin", "PluginID", plugin.ID}, taskInfo), "", nil
}
//adding LogEntries Colelction
logEntrydata := model.LogServices{
Ocontext: "/redfish/v1/$metadata#LogServiceCollection.LogServiceCollection",
Oid: "/redfish/v1/Managers/" + managerUUID + "/LogServices/SL",
Otype: "#LogService.v1_3_0.LogService",
Description: "Logs view",
Entries: &model.Entries{
Oid: "/redfish/v1/Managers/" + managerUUID + "/LogServices/SL/Entries",
},
ID: "SL",
Name: "Security Log",
OverWritePolicy: "WrapsWhenFull",
}
dbLogEntrydata, err := json.Marshal(logEntrydata)
if err != nil {
errMsg := "unable to marshal manager data: %v" + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, taskInfo), "", nil
}
lkey := "/redfish/v1/Managers/" + managerUUID + "/LogServices/SL"
logdbErr := agmodel.SavePluginManagerInfo([]byte(dbLogEntrydata), "LogServices", lkey)
if logdbErr != nil {
errMsg := logdbErr.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusConflict, response.ResourceAlreadyExists, errMsg, []interface{}{"Plugin", "PluginID", plugin.ID}, taskInfo), "", nil
}
// adding empty logservice entry collection
entriesdata := model.Collection{
ODataContext: "/redfish/v1/$metadata#LogServiceCollection.LogServiceCollection",
ODataID: "/redfish/v1/Managers/" + managerUUID + "/LogServices/SL/Entries",
ODataType: "#LogEntryCollection.LogEntryCollection",
Description: "Security Logs view",
Members: []*model.Link{},
MembersCount: 0,
Name: "Security Logs",
}
dbentriesdata, err := json.Marshal(entriesdata)
if err != nil {
errMsg := "unable to marshal manager data: %v" + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, taskInfo), "", nil
}
entrieskey := "/redfish/v1/Managers/" + managerUUID + "/LogServices/SL/Entries"
entriesdbErr := agmodel.SavePluginManagerInfo([]byte(dbentriesdata), "EntriesCollection", entrieskey)
if entriesdbErr != nil {
errMsg := entriesdbErr.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusConflict, response.ResourceAlreadyExists, errMsg, []interface{}{"Plugin", "PluginID", plugin.ID}, taskInfo), "", nil
}
// saving all plugin manager data
var listMembers = make([]agresponse.ListMember, 0)
for oid, data := range managersData {
dbErr := agmodel.SavePluginManagerInfo(updateManagerName(data, plugin.ID), "Managers", oid)
if dbErr != nil {
errMsg := dbErr.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusConflict, response.ResourceAlreadyExists, errMsg, []interface{}{"Plugin", "PluginID", plugin.ID}, taskInfo), "", nil
}
listMembers = append(listMembers, agresponse.ListMember{
OdataID: oid,
})
}
l.LogWithFields(ctx).Info("subscribing to EMB for plugin " + plugin.ID)
err = e.SubscribeToEMB(plugin.ID, queueList)
if err != nil {
l.LogWithFields(ctx).Error(err)
}
// store encrypted password
plugin.Password = ciphertext
plugin.ManagerUUID = managerUUID
// saving the pluginData
dbErr := agmodel.SavePluginData(plugin)
if dbErr != nil {
errMsg := "error: while saving the plugin data: " + dbErr.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, taskInfo), "", nil
}
resp.Header = map[string]string{
"Location": listMembers[0].OdataID,
}
var managersList = make([]string, 0)
for i := 0; i < len(listMembers); i++ {
managersList = append(managersList, listMembers[i].OdataID)
}
e.PublishEvent(ctx, managersList, "ManagerCollection")
resp.StatusCode = http.StatusCreated
l.LogWithFields(ctx).Info("sucessfully added plugin with the id ", cmVariants.PluginID)
phc := agcommon.PluginHealthCheckInterface{
DecryptPassword: common.DecryptWithPrivateKey,
}
phc.DupPluginConf()
_, topics := phc.GetPluginStatus(ctx, plugin)
PublishPluginStatusOKEvent(ctx, plugin.ID, topics)
return resp, managerUUID, ciphertext
}