Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 the original author or authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | package impl |
| 17 | |
| 18 | import ( |
| 19 | "errors" |
| 20 | "fmt" |
| 21 | "io/ioutil" |
| 22 | "log" |
| 23 | "net" |
| 24 | "net/http" |
| 25 | "strings" |
| 26 | |
| 27 | "gerrit.opencord.org/abstract-olt/internal/pkg/settings" |
| 28 | "gerrit.opencord.org/abstract-olt/models" |
| 29 | ) |
| 30 | |
| 31 | /* |
| 32 | ChangeXOSUserPassword - allows update of xos credentials |
| 33 | */ |
| 34 | func ChangeXOSUserPassword(clli string, xosUser string, xosPassword string) (bool, error) { |
| 35 | myChan := getSyncChannel() |
| 36 | <-myChan |
| 37 | defer done(myChan, true) |
| 38 | chassisMap := models.GetChassisMap() |
| 39 | chassisHolder := (*chassisMap)[clli] |
| 40 | if chassisHolder == nil { |
| 41 | errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli) |
| 42 | return false, errors.New(errString) |
| 43 | } |
| 44 | xosIP := chassisHolder.PhysicalChassis.XOSAddress.IP |
| 45 | xosPort := chassisHolder.PhysicalChassis.XOSAddress.Port |
| 46 | loginWorked := testLogin(xosUser, xosPassword, xosIP, xosPort) |
| 47 | if !loginWorked { |
| 48 | return false, errors.New("Unable to validate login when changing password") |
| 49 | } |
| 50 | |
| 51 | chassisHolder.PhysicalChassis.XOSUser = xosUser |
| 52 | chassisHolder.PhysicalChassis.XOSPassword = xosPassword |
| 53 | isDirty = true |
| 54 | return true, nil |
| 55 | |
| 56 | } |
| 57 | |
| 58 | func testLogin(xosUser string, xosPassword string, xosIP net.IP, xosPort int) bool { |
| 59 | if settings.GetDummy() { |
| 60 | return true |
| 61 | } |
Don Newton | 276cd1f | 2019-02-06 17:14:03 -0500 | [diff] [blame] | 62 | if settings.GetGrpc() { |
| 63 | return true |
| 64 | } |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 65 | var dummyYaml = ` |
| 66 | tosca_definitions_version: tosca_simple_yaml_1_0 |
| 67 | imports: |
| 68 | - custom_types/site.yaml |
| 69 | description: anything |
| 70 | topology_template: |
| 71 | node_templates: |
| 72 | mysite: |
| 73 | type: tosca.nodes.Site |
| 74 | properties: |
| 75 | must-exist: true |
| 76 | name: mysite |
| 77 | ` |
| 78 | client := &http.Client{} |
| 79 | requestList := fmt.Sprintf("http://%s:%d/run", xosIP, xosPort) |
| 80 | req, err := http.NewRequest("POST", requestList, strings.NewReader(dummyYaml)) |
| 81 | req.Header.Add("xos-username", xosUser) |
| 82 | req.Header.Add("xos-password", xosPassword) |
| 83 | resp, err := client.Do(req) |
| 84 | log.Printf("testLogin resp:%v", resp) |
| 85 | if err != nil { |
| 86 | log.Printf("Unable to validate XOS Login Information %v", err) |
| 87 | return false |
| 88 | } |
| 89 | defer resp.Body.Close() |
| 90 | |
| 91 | if resp.StatusCode == http.StatusOK { |
| 92 | bodyBytes, _ := ioutil.ReadAll(resp.Body) |
| 93 | bodyString := string(bodyBytes) |
| 94 | fmt.Println(bodyString) |
| 95 | return true |
| 96 | } |
| 97 | return false |
| 98 | |
| 99 | } |