github.com/braveheart12/insolar-09-08-19@v0.8.7/api/requester/config.go (about) 1 /* 2 * Copyright 2019 Insolar Technologies 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 17 package requester 18 19 import ( 20 "crypto" 21 "encoding/json" 22 "io/ioutil" 23 "os" 24 "path/filepath" 25 26 "github.com/insolar/insolar/platformpolicy" 27 28 "github.com/pkg/errors" 29 ) 30 31 // UserConfigJSON holds info about user 32 type UserConfigJSON struct { 33 PrivateKey string `json:"private_key"` 34 Caller string `json:"caller"` 35 privateKeyObject crypto.PrivateKey 36 } 37 38 // RequestConfigJSON holds info about request 39 type RequestConfigJSON struct { 40 Params []interface{} `json:"params"` 41 Method string `json:"method"` 42 } 43 44 func readFile(path string, configType interface{}) error { 45 var rawConf []byte 46 var err error 47 if path == "-" { 48 rawConf, err = ioutil.ReadAll(os.Stdin) 49 } else { 50 rawConf, err = ioutil.ReadFile(filepath.Clean(path)) 51 } 52 if err != nil { 53 return errors.Wrap(err, "[ readFile ] Problem with reading config") 54 } 55 56 err = json.Unmarshal(rawConf, &configType) 57 if err != nil { 58 return errors.Wrap(err, "[ readFile ] Problem with unmarshaling config") 59 } 60 61 return nil 62 } 63 64 // ReadUserConfigFromFile read user config from file 65 func ReadUserConfigFromFile(path string) (*UserConfigJSON, error) { 66 cfgJSON := &UserConfigJSON{} 67 err := readFile(path, cfgJSON) 68 if err != nil { 69 return nil, errors.Wrap(err, "[ readUserConfigFromFile ] ") 70 } 71 72 ks := platformpolicy.NewKeyProcessor() 73 74 if cfgJSON.PrivateKey == "" { 75 privKey, err := ks.GeneratePrivateKey() 76 if err != nil { 77 return nil, errors.Wrap(err, "[ readUserConfigFromFile ] ") 78 } 79 privKeyStr, err := ks.ExportPrivateKeyPEM(privKey) 80 if err != nil { 81 return nil, errors.Wrap(err, "[ readUserConfigFromFile ] ") 82 } 83 cfgJSON.PrivateKey = string(privKeyStr) 84 } 85 86 cfgJSON.privateKeyObject, err = ks.ImportPrivateKeyPEM([]byte(cfgJSON.PrivateKey)) 87 if err != nil { 88 return nil, errors.Wrap(err, "[ readUserConfigFromFile ] Problem with reading private key") 89 } 90 91 return cfgJSON, nil 92 } 93 94 // ReadRequestConfigFromFile read request config from file 95 func ReadRequestConfigFromFile(path string) (*RequestConfigJSON, error) { 96 rConfig := &RequestConfigJSON{} 97 err := readFile(path, rConfig) 98 if err != nil { 99 return nil, errors.Wrap(err, "[ readRequesterConfigFromFile ] ") 100 } 101 102 return rConfig, nil 103 } 104 105 // CreateUserConfig creates user config from arguments 106 func CreateUserConfig(caller string, privKey string) (*UserConfigJSON, error) { 107 userConfig := UserConfigJSON{PrivateKey: privKey, Caller: caller} 108 var err error 109 110 ks := platformpolicy.NewKeyProcessor() 111 userConfig.privateKeyObject, err = ks.ImportPrivateKeyPEM([]byte(privKey)) 112 return &userConfig, err 113 }