github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/cmd/swarm/access.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 package main 17 18 import ( 19 "crypto/rand" 20 "encoding/json" 21 "fmt" 22 "io" 23 "io/ioutil" 24 "strings" 25 26 "github.com/FusionFoundation/efsn/cmd/utils" 27 "github.com/FusionFoundation/efsn/swarm/api" 28 "github.com/FusionFoundation/efsn/swarm/api/client" 29 "gopkg.in/urfave/cli.v1" 30 ) 31 32 var salt = make([]byte, 32) 33 34 func init() { 35 if _, err := io.ReadFull(rand.Reader, salt); err != nil { 36 panic("reading from crypto/rand failed: " + err.Error()) 37 } 38 } 39 40 func accessNewPass(ctx *cli.Context) { 41 args := ctx.Args() 42 if len(args) != 1 { 43 utils.Fatalf("Expected 1 argument - the ref") 44 } 45 46 var ( 47 ae *api.AccessEntry 48 accessKey []byte 49 err error 50 ref = args[0] 51 password = getPassPhrase("", 0, makePasswordList(ctx)) 52 dryRun = ctx.Bool(SwarmDryRunFlag.Name) 53 ) 54 accessKey, ae, err = api.DoPassword(ctx, password, salt) 55 if err != nil { 56 utils.Fatalf("error getting session key: %v", err) 57 } 58 m, err := api.GenerateAccessControlManifest(ctx, ref, accessKey, ae) 59 if dryRun { 60 err = printManifests(m, nil) 61 if err != nil { 62 utils.Fatalf("had an error printing the manifests: %v", err) 63 } 64 } else { 65 err = uploadManifests(ctx, m, nil) 66 if err != nil { 67 utils.Fatalf("had an error uploading the manifests: %v", err) 68 } 69 } 70 } 71 72 func accessNewPK(ctx *cli.Context) { 73 args := ctx.Args() 74 if len(args) != 1 { 75 utils.Fatalf("Expected 1 argument - the ref") 76 } 77 78 var ( 79 ae *api.AccessEntry 80 sessionKey []byte 81 err error 82 ref = args[0] 83 privateKey = getPrivKey(ctx) 84 granteePublicKey = ctx.String(SwarmAccessGrantKeyFlag.Name) 85 dryRun = ctx.Bool(SwarmDryRunFlag.Name) 86 ) 87 sessionKey, ae, err = api.DoPK(ctx, privateKey, granteePublicKey, salt) 88 if err != nil { 89 utils.Fatalf("error getting session key: %v", err) 90 } 91 m, err := api.GenerateAccessControlManifest(ctx, ref, sessionKey, ae) 92 if dryRun { 93 err = printManifests(m, nil) 94 if err != nil { 95 utils.Fatalf("had an error printing the manifests: %v", err) 96 } 97 } else { 98 err = uploadManifests(ctx, m, nil) 99 if err != nil { 100 utils.Fatalf("had an error uploading the manifests: %v", err) 101 } 102 } 103 } 104 105 func accessNewACT(ctx *cli.Context) { 106 args := ctx.Args() 107 if len(args) != 1 { 108 utils.Fatalf("Expected 1 argument - the ref") 109 } 110 111 var ( 112 ae *api.AccessEntry 113 actManifest *api.Manifest 114 accessKey []byte 115 err error 116 ref = args[0] 117 pkGrantees = []string{} 118 passGrantees = []string{} 119 pkGranteesFilename = ctx.String(SwarmAccessGrantKeysFlag.Name) 120 passGranteesFilename = ctx.String(utils.PasswordFileFlag.Name) 121 privateKey = getPrivKey(ctx) 122 dryRun = ctx.Bool(SwarmDryRunFlag.Name) 123 ) 124 if pkGranteesFilename == "" && passGranteesFilename == "" { 125 utils.Fatalf("you have to provide either a grantee public-keys file or an encryption passwords file (or both)") 126 } 127 128 if pkGranteesFilename != "" { 129 bytes, err := ioutil.ReadFile(pkGranteesFilename) 130 if err != nil { 131 utils.Fatalf("had an error reading the grantee public key list") 132 } 133 pkGrantees = strings.Split(string(bytes), "\n") 134 } 135 136 if passGranteesFilename != "" { 137 bytes, err := ioutil.ReadFile(passGranteesFilename) 138 if err != nil { 139 utils.Fatalf("could not read password filename: %v", err) 140 } 141 passGrantees = strings.Split(string(bytes), "\n") 142 } 143 accessKey, ae, actManifest, err = api.DoACT(ctx, privateKey, salt, pkGrantees, passGrantees) 144 if err != nil { 145 utils.Fatalf("error generating ACT manifest: %v", err) 146 } 147 148 if err != nil { 149 utils.Fatalf("error getting session key: %v", err) 150 } 151 m, err := api.GenerateAccessControlManifest(ctx, ref, accessKey, ae) 152 if err != nil { 153 utils.Fatalf("error generating root access manifest: %v", err) 154 } 155 156 if dryRun { 157 err = printManifests(m, actManifest) 158 if err != nil { 159 utils.Fatalf("had an error printing the manifests: %v", err) 160 } 161 } else { 162 err = uploadManifests(ctx, m, actManifest) 163 if err != nil { 164 utils.Fatalf("had an error uploading the manifests: %v", err) 165 } 166 } 167 } 168 169 func printManifests(rootAccessManifest, actManifest *api.Manifest) error { 170 js, err := json.Marshal(rootAccessManifest) 171 if err != nil { 172 return err 173 } 174 fmt.Println(string(js)) 175 176 if actManifest != nil { 177 js, err := json.Marshal(actManifest) 178 if err != nil { 179 return err 180 } 181 fmt.Println(string(js)) 182 } 183 return nil 184 } 185 186 func uploadManifests(ctx *cli.Context, rootAccessManifest, actManifest *api.Manifest) error { 187 bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") 188 client := client.NewClient(bzzapi) 189 190 var ( 191 key string 192 err error 193 ) 194 if actManifest != nil { 195 key, err = client.UploadManifest(actManifest, false) 196 if err != nil { 197 return err 198 } 199 200 rootAccessManifest.Entries[0].Access.Act = key 201 } 202 key, err = client.UploadManifest(rootAccessManifest, false) 203 if err != nil { 204 return err 205 } 206 fmt.Println(key) 207 return nil 208 } 209 210 // makePasswordList reads password lines from the file specified by the global --password flag 211 // and also by the same subcommand --password flag. 212 // This function ia a fork of utils.MakePasswordList to lookup cli context for subcommand. 213 // Function ctx.SetGlobal is not setting the global flag value that can be accessed 214 // by ctx.GlobalString using the current version of cli package. 215 func makePasswordList(ctx *cli.Context) []string { 216 path := ctx.GlobalString(utils.PasswordFileFlag.Name) 217 if path == "" { 218 path = ctx.String(utils.PasswordFileFlag.Name) 219 if path == "" { 220 return nil 221 } 222 } 223 text, err := ioutil.ReadFile(path) 224 if err != nil { 225 utils.Fatalf("Failed to read password file: %v", err) 226 } 227 lines := strings.Split(string(text), "\n") 228 // Sanitise DOS line endings. 229 for i := range lines { 230 lines[i] = strings.TrimRight(lines[i], "\r") 231 } 232 return lines 233 }