Skip to content

Commit

Permalink
Add support for python3 (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarguelloF authored and jhaals committed Feb 20, 2018
1 parent 7c4aedc commit 67e25a7
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 23 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

[*.py]
indent_size = 4

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_size = 4
indent_style = tab
13 changes: 6 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ sudo: required
language: python
services:
- docker
python:
- "2.7"
before_install:
- docker build -t ansible/test -f tests/Dockerfile .
install:
pip install flake8
- docker build -t ansible_vault/py2 -f tests/Dockerfile.py2 .
- docker build -t ansible_vault/py3 -f tests/Dockerfile.py3 .
script:
- flake8
- docker run -it ansible/test
- docker run -it ansible_vault/py2 flake8
- docker run -it ansible_vault/py2 ./tests/test.sh
- docker run -it ansible_vault/py3 flake8
- docker run -it ansible_vault/py3 ./tests/test.sh
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: build test

test: build
docker run -it ansible_vault/py2 flake8
docker run -it ansible_vault/py2 ./tests/test.sh
docker run -it ansible_vault/py3 flake8
docker run -it ansible_vault/py3 ./tests/test.sh

build:
docker build -t ansible_vault/py2 -f tests/Dockerfile.py2 .
docker build -t ansible_vault/py3 -f tests/Dockerfile.py3 .
9 changes: 0 additions & 9 deletions tests/Dockerfile

This file was deleted.

15 changes: 15 additions & 0 deletions tests/Dockerfile.py2
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM python:2.7-slim

ENV VAULT_VERSION 0.9.3

RUN apt-get update && \
apt-get install unzip curl -y

RUN curl -LO https://releases.hashicorp.com/vault/${VAULT_VERSION}/vault_${VAULT_VERSION}_linux_amd64.zip && \
unzip vault_${VAULT_VERSION}_linux_amd64.zip -d /bin

RUN pip install ansible flake8
COPY . ansible-vault
WORKDIR ./ansible-vault

CMD ["./tests/test.sh"]
16 changes: 16 additions & 0 deletions tests/Dockerfile.py3
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.6-slim

ENV VAULT_VERSION 0.9.3

RUN apt-get update && \
apt-get install unzip curl -y

RUN curl -LO https://releases.hashicorp.com/vault/${VAULT_VERSION}/vault_${VAULT_VERSION}_linux_amd64.zip && \
unzip vault_${VAULT_VERSION}_linux_amd64.zip -d /bin

RUN pip install --upgrade setuptools
RUN pip install ansible flake8
COPY . ansible-vault
WORKDIR ./ansible-vault

CMD ["./tests/test.sh"]
2 changes: 1 addition & 1 deletion tests/ansible.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[defaults]
lookup_plugins = ../
lookup_plugins = ../
2 changes: 1 addition & 1 deletion tests/test-playbook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
- name: Get custom field
debug: msg="{{ lookup('vault', 'secret/user', 'password') }}"
- name: Get certificate
debug: msg="{{ lookup('vault', 'pki/issue/example-dot-com common_name=foo.example.com format=pem_bundle').certificate }}"
debug: msg="{{ lookup('vault', 'pki/issue/example-dot-com common_name=foo.example.com format=pem_bundle').certificate }}"
4 changes: 2 additions & 2 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ERR=0
vault server -dev &
VAULT_PID=$!
sleep 1
vault mount pki
vault secrets enable pki
vault write pki/root/generate/internal common_name=myvault.com
vault write pki/config/urls issuing_certificates="http://127.0.0.1:8200/v1/pki/ca" crl_distribution_points="http://127.0.0.1:8200/v1/pki/crl"
vault write pki/roles/example-dot-com \
Expand All @@ -21,4 +21,4 @@ if [ $? -ne 0 ]; then
ERR=1
fi
kill $VAULT_PID
exit $ERR
exit $ERR
11 changes: 8 additions & 3 deletions vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import errno
import ssl
import shlex
import codecs
from distutils.version import StrictVersion
from sys import version_info

Expand Down Expand Up @@ -168,12 +169,14 @@ def _fetch_client_token(self, cafile, capath, url, data, cahostverify, skipverif
context.check_hostname = cahostverify
elif skipverify:
context = ssl._create_unverified_context()
data = data.encode('utf-8') if data else None
req = urllib2.Request(url, json.dumps(data))
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, context=context) if context else urllib2.urlopen(req)
except Exception as ex:
raise AnsibleError('Unable to retrieve personal token from vault: %s' % (ex))
result = json.loads(response.read())
reader = codecs.getreader("utf-8")
result = json.load(reader(response))
return result

def _fetch_secret(self, cafile, capath, data, key, vault_token, url, cahostverify, skipverify):
Expand All @@ -185,15 +188,17 @@ def _fetch_secret(self, cafile, capath, data, key, vault_token, url, cahostverif
elif skipverify:
context = ssl._create_unverified_context()
request_url = urljoin(url, "v1/%s" % (key))
data = data.encode('utf-8') if data else None
req = urllib2.Request(request_url, data)
req.add_header('X-Vault-Token', vault_token)
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, context=context) if context else urllib2.urlopen(req)
except Exception as ex:
raise AnsibleError('Unable to read %s from vault: %s' % (key, ex))
body = response.read()
reader = codecs.getreader("utf-8")
body = reader(response)
if response.headers.get('Content-Type') == 'application/json':
body = json.loads(body)
body = json.load(body)
return body

def _verify_python_version(self, key, cafile, capath, cahostverify):
Expand Down

0 comments on commit 67e25a7

Please sign in to comment.