access.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. import (
  18. "crypto/rand"
  19. "encoding/json"
  20. "fmt"
  21. "io"
  22. "io/ioutil"
  23. "strings"
  24. "github.com/ethereum/go-ethereum/cmd/utils"
  25. "github.com/ethereum/go-ethereum/swarm/api"
  26. "github.com/ethereum/go-ethereum/swarm/api/client"
  27. "gopkg.in/urfave/cli.v1"
  28. )
  29. var salt = make([]byte, 32)
  30. func init() {
  31. if _, err := io.ReadFull(rand.Reader, salt); err != nil {
  32. panic("reading from crypto/rand failed: " + err.Error())
  33. }
  34. }
  35. func accessNewPass(ctx *cli.Context) {
  36. args := ctx.Args()
  37. if len(args) != 1 {
  38. utils.Fatalf("Expected 1 argument - the ref")
  39. }
  40. var (
  41. ae *api.AccessEntry
  42. accessKey []byte
  43. err error
  44. ref = args[0]
  45. password = getPassPhrase("", 0, makePasswordList(ctx))
  46. dryRun = ctx.Bool(SwarmDryRunFlag.Name)
  47. )
  48. accessKey, ae, err = api.DoPasswordNew(ctx, password, salt)
  49. if err != nil {
  50. utils.Fatalf("error getting session key: %v", err)
  51. }
  52. m, err := api.GenerateAccessControlManifest(ctx, ref, accessKey, ae)
  53. if dryRun {
  54. err = printManifests(m, nil)
  55. if err != nil {
  56. utils.Fatalf("had an error printing the manifests: %v", err)
  57. }
  58. } else {
  59. utils.Fatalf("uploading manifests")
  60. err = uploadManifests(ctx, m, nil)
  61. if err != nil {
  62. utils.Fatalf("had an error uploading the manifests: %v", err)
  63. }
  64. }
  65. }
  66. func accessNewPK(ctx *cli.Context) {
  67. args := ctx.Args()
  68. if len(args) != 1 {
  69. utils.Fatalf("Expected 1 argument - the ref")
  70. }
  71. var (
  72. ae *api.AccessEntry
  73. sessionKey []byte
  74. err error
  75. ref = args[0]
  76. privateKey = getPrivKey(ctx)
  77. granteePublicKey = ctx.String(SwarmAccessGrantKeyFlag.Name)
  78. dryRun = ctx.Bool(SwarmDryRunFlag.Name)
  79. )
  80. sessionKey, ae, err = api.DoPKNew(ctx, privateKey, granteePublicKey, salt)
  81. if err != nil {
  82. utils.Fatalf("error getting session key: %v", err)
  83. }
  84. m, err := api.GenerateAccessControlManifest(ctx, ref, sessionKey, ae)
  85. if dryRun {
  86. err = printManifests(m, nil)
  87. if err != nil {
  88. utils.Fatalf("had an error printing the manifests: %v", err)
  89. }
  90. } else {
  91. err = uploadManifests(ctx, m, nil)
  92. if err != nil {
  93. utils.Fatalf("had an error uploading the manifests: %v", err)
  94. }
  95. }
  96. }
  97. func accessNewACT(ctx *cli.Context) {
  98. args := ctx.Args()
  99. if len(args) != 1 {
  100. utils.Fatalf("Expected 1 argument - the ref")
  101. }
  102. var (
  103. ae *api.AccessEntry
  104. actManifest *api.Manifest
  105. accessKey []byte
  106. err error
  107. ref = args[0]
  108. grantees = []string{}
  109. actFilename = ctx.String(SwarmAccessGrantKeysFlag.Name)
  110. privateKey = getPrivKey(ctx)
  111. dryRun = ctx.Bool(SwarmDryRunFlag.Name)
  112. )
  113. bytes, err := ioutil.ReadFile(actFilename)
  114. if err != nil {
  115. utils.Fatalf("had an error reading the grantee public key list")
  116. }
  117. grantees = strings.Split(string(bytes), "\n")
  118. accessKey, ae, actManifest, err = api.DoACTNew(ctx, privateKey, salt, grantees)
  119. if err != nil {
  120. utils.Fatalf("error generating ACT manifest: %v", err)
  121. }
  122. if err != nil {
  123. utils.Fatalf("error getting session key: %v", err)
  124. }
  125. m, err := api.GenerateAccessControlManifest(ctx, ref, accessKey, ae)
  126. if err != nil {
  127. utils.Fatalf("error generating root access manifest: %v", err)
  128. }
  129. if dryRun {
  130. err = printManifests(m, actManifest)
  131. if err != nil {
  132. utils.Fatalf("had an error printing the manifests: %v", err)
  133. }
  134. } else {
  135. err = uploadManifests(ctx, m, actManifest)
  136. if err != nil {
  137. utils.Fatalf("had an error uploading the manifests: %v", err)
  138. }
  139. }
  140. }
  141. func printManifests(rootAccessManifest, actManifest *api.Manifest) error {
  142. js, err := json.Marshal(rootAccessManifest)
  143. if err != nil {
  144. return err
  145. }
  146. fmt.Println(string(js))
  147. if actManifest != nil {
  148. js, err := json.Marshal(actManifest)
  149. if err != nil {
  150. return err
  151. }
  152. fmt.Println(string(js))
  153. }
  154. return nil
  155. }
  156. func uploadManifests(ctx *cli.Context, rootAccessManifest, actManifest *api.Manifest) error {
  157. bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
  158. client := client.NewClient(bzzapi)
  159. var (
  160. key string
  161. err error
  162. )
  163. if actManifest != nil {
  164. key, err = client.UploadManifest(actManifest, false)
  165. if err != nil {
  166. return err
  167. }
  168. rootAccessManifest.Entries[0].Access.Act = key
  169. }
  170. key, err = client.UploadManifest(rootAccessManifest, false)
  171. if err != nil {
  172. return err
  173. }
  174. fmt.Println(key)
  175. return nil
  176. }
  177. // makePasswordList reads password lines from the file specified by the global --password flag
  178. // and also by the same subcommand --password flag.
  179. // This function ia a fork of utils.MakePasswordList to lookup cli context for subcommand.
  180. // Function ctx.SetGlobal is not setting the global flag value that can be accessed
  181. // by ctx.GlobalString using the current version of cli package.
  182. func makePasswordList(ctx *cli.Context) []string {
  183. path := ctx.GlobalString(utils.PasswordFileFlag.Name)
  184. if path == "" {
  185. path = ctx.String(utils.PasswordFileFlag.Name)
  186. if path == "" {
  187. return nil
  188. }
  189. }
  190. text, err := ioutil.ReadFile(path)
  191. if err != nil {
  192. utils.Fatalf("Failed to read password file: %v", err)
  193. }
  194. lines := strings.Split(string(text), "\n")
  195. // Sanitise DOS line endings.
  196. for i := range lines {
  197. lines[i] = strings.TrimRight(lines[i], "\r")
  198. }
  199. return lines
  200. }