From 8943e8669f7d3b14e6e75322c311fff6af207840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Fri, 26 Apr 2019 15:24:37 +0200 Subject: [PATCH] improve account creation, allow specifying account json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- defaults/main.yml | 12 ++++-- tasks/container.yml | 4 +- tasks/generate.yml | 102 ++++++++++++++++++++++++++++---------------- 3 files changed, 76 insertions(+), 42 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index ec5b64b..b5b8744 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,7 +1,8 @@ --- -# Security -geth_account_address: ~ -geth_account_password: ~ +# Security, json is optional +geth_account_addr: ~ +geth_account_pass: ~ +geth_account_json: ~ # Container config geth_cont_image: 'ethereum/client-go:v1.8.27' @@ -10,6 +11,11 @@ geth_cont_name: 'geth' geth_cont_vol: '/docker/{{ geth_cont_name }}' geth_data_path: '{{ geth_cont_vol }}/data' geth_keys_path: '{{ geth_cont_vol }}/keys' +# account +geth_account_json_file: '{{ geth_keys_path }}/account.json' +geth_account_pass_file: '{{ geth_keys_path }}/account.pass' +geth_account_addr_file: '{{ geth_keys_path }}/account.addr' + geth_enode_file: '{{ geth_keys_path }}/enode' geth_sync_check_script: '/usr/local/bin/check_geth_sync.sh' # RPC port of administration diff --git a/tasks/container.yml b/tasks/container.yml index 9d5cce7..5935905 100644 --- a/tasks/container.yml +++ b/tasks/container.yml @@ -36,9 +36,9 @@ --cache={{ geth_cache }} --port={{ geth_port }} --nat=extip:{{ geth_public_addr }} - --unlock={{ geth_account_address }} + --unlock={{ geth_account_addr }} --datadir=/data - --password=/keys/password + --password=/keys/account.pass --keystore=/keys --rpc --rpcaddr=0.0.0.0 diff --git a/tasks/generate.yml b/tasks/generate.yml index c90c257..0996131 100644 --- a/tasks/generate.yml +++ b/tasks/generate.yml @@ -4,50 +4,78 @@ path: '{{ geth_keys_path }}' state: directory -- name: Geth | Find all account files - find: - paths: '{{ geth_keys_path }}' - patterns: 'UTC--*' - file_type: file - register: account_files +- name: Geth | Check if account file exists + stat: + path: '{{ geth_account_json_file }}' + register: account_file + when: geth_account_json is not defined -- name: Geth | Set password to be empty +- name: Geth | Create password file copy: - dest: '{{ geth_keys_path }}/password' - content: '{{ geth_account_password | mandatory }}' + dest: '{{ geth_account_pass_file }}' + content: '{{ geth_account_pass | mandatory }}' + owner: dockremap + group: docker mode: 0600 +- name: Geth | Write account JSON + copy: + dest: '{{ geth_account_json_file }}' + content: '{{ geth_account_json }}' + owner: dockremap + group: docker + mode: 0640 + when: > + geth_account_json is defined and + geth_account_addr is defined + - name: Geth | Generate account - docker_container: - name: '{{ geth_cont_name }}' - image: '{{ geth_cont_image }}' - pull: true - auto_remove: yes - command: | - account new - --keystore=/keys - --password=/keys/password - volumes: - - '{{ geth_cont_vol }}/keys:/keys:rw' - when: account_files.files | length == 0 + block: + - name: Geth | Generate account JSON + docker_container: + name: '{{ geth_cont_name }}' + image: '{{ geth_cont_image }}' + pull: true + auto_remove: yes + command: | + account new + --keystore=/keys + --password=/keys/account.pass + volumes: + - '{{ geth_cont_vol }}/keys:/keys:rw' + + - name: Geth | Find newly generate account file + find: + paths: '{{ geth_keys_path }}' + patterns: 'UTC--*' + file_type: file + register: found_account_files + + - name: Geth | Rename generated account file + command: 'mv {{ found_account_files.files[0].path }} {{ geth_account_json_file }}' -- name: Geth | Find all account files - find: - paths: '{{ geth_keys_path }}' - patterns: 'UTC--*' - file_type: file - register: account_files + - name: Geth | Save account address + slurp: + src: '{{ geth_account_json_file }}' + register: geth_account + + - name: Geth | Extract account address + set_fact: + geth_account_addr: '{{ (geth_account.content | b64decode | from_json)["address"] }}' + when: > + geth_account_json is not defined and + geth_account_addr is not defined and + account_file.stat.exists == False -- name: Geth | Save account address - slurp: - src: '{{ account_files.files[0].path }}' - register: geth_account - -- name: Geth | Extract account address - set_fact: - geth_account_address: '{{ (geth_account.content | b64decode | from_json)["address"] }}' +- name: Geth | Verify account data is available + assert: + that: + - geth_account_json is defined + - geth_account_addr is defined - name: Geth | Save account address to file copy: - dest: '{{ geth_keys_path }}/address' - content: '{{ geth_account_address | mandatory }}' + dest: '{{ geth_account_addr_file }}' + content: '{{ geth_account_addr | mandatory }}' + owner: dockremap + group: docker