access.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 (
  30. salt = make([]byte, 32)
  31. accessCommand = cli.Command{
  32. CustomHelpTemplate: helpTemplate,
  33. Name: "access",
  34. Usage: "encrypts a reference and embeds it into a root manifest",
  35. ArgsUsage: "<ref>",
  36. Description: "encrypts a reference and embeds it into a root manifest",
  37. Subcommands: []cli.Command{
  38. {
  39. CustomHelpTemplate: helpTemplate,
  40. Name: "new",
  41. Usage: "encrypts a reference and embeds it into a root manifest",
  42. ArgsUsage: "<ref>",
  43. Description: "encrypts a reference and embeds it into a root access manifest and prints the resulting manifest",
  44. Subcommands: []cli.Command{
  45. {
  46. Action: accessNewPass,
  47. CustomHelpTemplate: helpTemplate,
  48. Flags: []cli.Flag{
  49. utils.PasswordFileFlag,
  50. SwarmDryRunFlag,
  51. },
  52. Name: "pass",
  53. Usage: "encrypts a reference with a password and embeds it into a root manifest",
  54. ArgsUsage: "<ref>",
  55. Description: "encrypts a reference and embeds it into a root access manifest and prints the resulting manifest",
  56. },
  57. {
  58. Action: accessNewPK,
  59. CustomHelpTemplate: helpTemplate,
  60. Flags: []cli.Flag{
  61. utils.PasswordFileFlag,
  62. SwarmDryRunFlag,
  63. SwarmAccessGrantKeyFlag,
  64. },
  65. Name: "pk",
  66. Usage: "encrypts a reference with the node's private key and a given grantee's public key and embeds it into a root manifest",
  67. ArgsUsage: "<ref>",
  68. Description: "encrypts a reference and embeds it into a root access manifest and prints the resulting manifest",
  69. },
  70. {
  71. Action: accessNewACT,
  72. CustomHelpTemplate: helpTemplate,
  73. Flags: []cli.Flag{
  74. SwarmAccessGrantKeysFlag,
  75. SwarmDryRunFlag,
  76. utils.PasswordFileFlag,
  77. },
  78. Name: "act",
  79. Usage: "encrypts a reference with the node's private key and a given grantee's public key and embeds it into a root manifest",
  80. ArgsUsage: "<ref>",
  81. Description: "encrypts a reference and embeds it into a root access manifest and prints the resulting manifest",
  82. },
  83. },
  84. },
  85. },
  86. }
  87. )
  88. func init() {
  89. if _, err := io.ReadFull(rand.Reader, salt); err != nil {
  90. panic("reading from crypto/rand failed: " + err.Error())
  91. }
  92. }
  93. func accessNewPass(ctx *cli.Context) {
  94. args := ctx.Args()
  95. if len(args) != 1 {
  96. utils.Fatalf("Expected 1 argument - the ref")
  97. }
  98. var (
  99. ae *api.AccessEntry
  100. accessKey []byte
  101. err error
  102. ref = args[0]
  103. password = getPassPhrase("", 0, makePasswordList(ctx))
  104. dryRun = ctx.Bool(SwarmDryRunFlag.Name)
  105. )
  106. accessKey, ae, err = api.DoPassword(ctx, password, salt)
  107. if err != nil {
  108. utils.Fatalf("error getting session key: %v", err)
  109. }
  110. m, err := api.GenerateAccessControlManifest(ctx, ref, accessKey, ae)
  111. if err != nil {
  112. utils.Fatalf("had an error generating the manifest: %v", err)
  113. }
  114. if dryRun {
  115. err = printManifests(m, nil)
  116. if err != nil {
  117. utils.Fatalf("had an error printing the manifests: %v", err)
  118. }
  119. } else {
  120. err = uploadManifests(ctx, m, nil)
  121. if err != nil {
  122. utils.Fatalf("had an error uploading the manifests: %v", err)
  123. }
  124. }
  125. }
  126. func accessNewPK(ctx *cli.Context) {
  127. args := ctx.Args()
  128. if len(args) != 1 {
  129. utils.Fatalf("Expected 1 argument - the ref")
  130. }
  131. var (
  132. ae *api.AccessEntry
  133. sessionKey []byte
  134. err error
  135. ref = args[0]
  136. privateKey = getPrivKey(ctx)
  137. granteePublicKey = ctx.String(SwarmAccessGrantKeyFlag.Name)
  138. dryRun = ctx.Bool(SwarmDryRunFlag.Name)
  139. )
  140. sessionKey, ae, err = api.DoPK(ctx, privateKey, granteePublicKey, salt)
  141. if err != nil {
  142. utils.Fatalf("error getting session key: %v", err)
  143. }
  144. m, err := api.GenerateAccessControlManifest(ctx, ref, sessionKey, ae)
  145. if err != nil {
  146. utils.Fatalf("had an error generating the manifest: %v", err)
  147. }
  148. if dryRun {
  149. err = printManifests(m, nil)
  150. if err != nil {
  151. utils.Fatalf("had an error printing the manifests: %v", err)
  152. }
  153. } else {
  154. err = uploadManifests(ctx, m, nil)
  155. if err != nil {
  156. utils.Fatalf("had an error uploading the manifests: %v", err)
  157. }
  158. }
  159. }
  160. func accessNewACT(ctx *cli.Context) {
  161. args := ctx.Args()
  162. if len(args) != 1 {
  163. utils.Fatalf("Expected 1 argument - the ref")
  164. }
  165. var (
  166. ae *api.AccessEntry
  167. actManifest *api.Manifest
  168. accessKey []byte
  169. err error
  170. ref = args[0]
  171. pkGrantees []string
  172. passGrantees []string
  173. pkGranteesFilename = ctx.String(SwarmAccessGrantKeysFlag.Name)
  174. passGranteesFilename = ctx.String(utils.PasswordFileFlag.Name)
  175. privateKey = getPrivKey(ctx)
  176. dryRun = ctx.Bool(SwarmDryRunFlag.Name)
  177. )
  178. if pkGranteesFilename == "" && passGranteesFilename == "" {
  179. utils.Fatalf("you have to provide either a grantee public-keys file or an encryption passwords file (or both)")
  180. }
  181. if pkGranteesFilename != "" {
  182. bytes, err := ioutil.ReadFile(pkGranteesFilename)
  183. if err != nil {
  184. utils.Fatalf("had an error reading the grantee public key list")
  185. }
  186. pkGrantees = strings.Split(strings.Trim(string(bytes), "\n"), "\n")
  187. }
  188. if passGranteesFilename != "" {
  189. bytes, err := ioutil.ReadFile(passGranteesFilename)
  190. if err != nil {
  191. utils.Fatalf("could not read password filename: %v", err)
  192. }
  193. passGrantees = strings.Split(strings.Trim(string(bytes), "\n"), "\n")
  194. }
  195. accessKey, ae, actManifest, err = api.DoACT(ctx, privateKey, salt, pkGrantees, passGrantees)
  196. if err != nil {
  197. utils.Fatalf("error generating ACT manifest: %v", err)
  198. }
  199. if err != nil {
  200. utils.Fatalf("error getting session key: %v", err)
  201. }
  202. m, err := api.GenerateAccessControlManifest(ctx, ref, accessKey, ae)
  203. if err != nil {
  204. utils.Fatalf("error generating root access manifest: %v", err)
  205. }
  206. if dryRun {
  207. err = printManifests(m, actManifest)
  208. if err != nil {
  209. utils.Fatalf("had an error printing the manifests: %v", err)
  210. }
  211. } else {
  212. err = uploadManifests(ctx, m, actManifest)
  213. if err != nil {
  214. utils.Fatalf("had an error uploading the manifests: %v", err)
  215. }
  216. }
  217. }
  218. func printManifests(rootAccessManifest, actManifest *api.Manifest) error {
  219. js, err := json.Marshal(rootAccessManifest)
  220. if err != nil {
  221. return err
  222. }
  223. fmt.Println(string(js))
  224. if actManifest != nil {
  225. js, err := json.Marshal(actManifest)
  226. if err != nil {
  227. return err
  228. }
  229. fmt.Println(string(js))
  230. }
  231. return nil
  232. }
  233. func uploadManifests(ctx *cli.Context, rootAccessManifest, actManifest *api.Manifest) error {
  234. bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
  235. client := client.NewClient(bzzapi)
  236. var (
  237. key string
  238. err error
  239. )
  240. if actManifest != nil {
  241. key, err = client.UploadManifest(actManifest, false)
  242. if err != nil {
  243. return err
  244. }
  245. rootAccessManifest.Entries[0].Access.Act = key
  246. }
  247. key, err = client.UploadManifest(rootAccessManifest, false)
  248. if err != nil {
  249. return err
  250. }
  251. fmt.Println(key)
  252. return nil
  253. }
  254. // makePasswordList reads password lines from the file specified by the global --password flag
  255. // and also by the same subcommand --password flag.
  256. // This function ia a fork of utils.MakePasswordList to lookup cli context for subcommand.
  257. // Function ctx.SetGlobal is not setting the global flag value that can be accessed
  258. // by ctx.GlobalString using the current version of cli package.
  259. func makePasswordList(ctx *cli.Context) []string {
  260. path := ctx.GlobalString(utils.PasswordFileFlag.Name)
  261. if path == "" {
  262. path = ctx.String(utils.PasswordFileFlag.Name)
  263. if path == "" {
  264. return nil
  265. }
  266. }
  267. text, err := ioutil.ReadFile(path)
  268. if err != nil {
  269. utils.Fatalf("Failed to read password file: %v", err)
  270. }
  271. lines := strings.Split(string(text), "\n")
  272. // Sanitise DOS line endings.
  273. for i := range lines {
  274. lines[i] = strings.TrimRight(lines[i], "\r")
  275. }
  276. return lines
  277. }