Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor consul_session to support authentication with tokens #6755

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/BOTMETA.yml
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ files:
ignore: resmo
maintainers: dmtrs
$modules/consul:
ignore: colin-nolan
ignore: colin-nolan Hakon
maintainers: $team_consul
$modules/copr.py:
maintainers: schlupov
Expand Down
169 changes: 133 additions & 36 deletions plugins/modules/consul_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- requests
author:
- Steve Gargan (@sgargan)
- Håkon Lerring (@Hakon)
extends_documentation_fragment:
- community.general.attributes
attributes:
Expand Down Expand Up @@ -147,14 +148,14 @@
ttl: 600 # sec
'''

from ansible.module_utils.basic import AnsibleModule

try:
import consul
import requests
from requests.exceptions import ConnectionError
python_consul_installed = True
has_requests = True
except ImportError:
python_consul_installed = False

from ansible.module_utils.basic import AnsibleModule
has_requests = False


def execute(module):
Expand All @@ -169,30 +170,89 @@ def execute(module):
remove_session(module)


class RequestError(Exception):
pass


def handle_consul_response_error(response):
if 400 <= response.status_code < 600:
raise RequestError('%d %s' % (response.status_code, response.content))


def get_consul_url(module):
return '%s://%s:%s/v1' % (module.params.get('scheme'),
module.params.get('host'), module.params.get('port'))


def get_auth_headers(module):
if 'token' in module.params and module.params.get('token') is not None:
return {'X-Consul-Token': module.params.get('token')}
else:
return {}


def list_sessions(module, datacenter):
url = '%s/session/list' % get_consul_url(module)
headers = get_auth_headers(module)
response = requests.get(
url,
headers=headers,
params={
'dc': datacenter},
verify=module.params.get('validate_certs'))
handle_consul_response_error(response)
return response.json()


def list_sessions_for_node(module, node, datacenter):
url = '%s/session/node/%s' % (get_consul_url(module), node)
headers = get_auth_headers(module)
response = requests.get(
url,
headers=headers,
params={
'dc': datacenter},
verify=module.params.get('validate_certs'))
handle_consul_response_error(response)
return response.json()


def get_session_info(module, session_id, datacenter):
url = '%s/session/info/%s' % (get_consul_url(module), session_id)
headers = get_auth_headers(module)
response = requests.get(
url,
headers=headers,
params={
'dc': datacenter},
verify=module.params.get('validate_certs'))
handle_consul_response_error(response)
return response.json()


def lookup_sessions(module):

datacenter = module.params.get('datacenter')

state = module.params.get('state')
consul_client = get_consul_api(module)
try:
if state == 'list':
sessions_list = consul_client.session.list(dc=datacenter)
sessions_list = list_sessions(module, datacenter)
# Ditch the index, this can be grabbed from the results
if sessions_list and len(sessions_list) >= 2:
sessions_list = sessions_list[1]
module.exit_json(changed=True,
sessions=sessions_list)
elif state == 'node':
node = module.params.get('node')
sessions = consul_client.session.node(node, dc=datacenter)
sessions = list_sessions_for_node(module, node, datacenter)
module.exit_json(changed=True,
node=node,
sessions=sessions)
elif state == 'info':
session_id = module.params.get('id')

session_by_id = consul_client.session.info(session_id, dc=datacenter)
session_by_id = get_session_info(module, session_id, datacenter)
module.exit_json(changed=True,
session_id=session_id,
sessions=session_by_id)
Expand All @@ -201,6 +261,31 @@ def lookup_sessions(module):
module.fail_json(msg="Could not retrieve session info %s" % e)


def create_session(module, name, behavior, ttl, node,
lock_delay, datacenter, checks):
url = '%s/session/create' % get_consul_url(module)
headers = get_auth_headers(module)
create_data = {
"LockDelay": lock_delay,
"Node": node,
"Name": name,
"Checks": checks,
"Behavior": behavior,
}
if ttl is not None:
create_data["TTL"] = "%ss" % str(ttl) # TTL is in seconds
response = requests.put(
url,
headers=headers,
params={
'dc': datacenter},
json=create_data,
verify=module.params.get('validate_certs'))
handle_consul_response_error(response)
create_session_response_dict = response.json()
return create_session_response_dict["ID"]


def update_session(module):

name = module.params.get('name')
Expand All @@ -211,18 +296,16 @@ def update_session(module):
behavior = module.params.get('behavior')
ttl = module.params.get('ttl')

consul_client = get_consul_api(module)

try:
session = consul_client.session.create(
name=name,
behavior=behavior,
ttl=ttl,
node=node,
lock_delay=delay,
dc=datacenter,
checks=checks
)
session = create_session(module,
name=name,
behavior=behavior,
ttl=ttl,
node=node,
lock_delay=delay,
datacenter=datacenter,
checks=checks
)
module.exit_json(changed=True,
session_id=session,
name=name,
Expand All @@ -235,13 +318,22 @@ def update_session(module):
module.fail_json(msg="Could not create/update session %s" % e)


def destroy_session(module, session_id):
url = '%s/session/destroy/%s' % (get_consul_url(module), session_id)
headers = get_auth_headers(module)
response = requests.put(
url,
headers=headers,
verify=module.params.get('validate_certs'))
handle_consul_response_error(response)
return response.content == "true"


def remove_session(module):
session_id = module.params.get('id')

consul_client = get_consul_api(module)

try:
consul_client.session.destroy(session_id)
destroy_session(module, session_id)

module.exit_json(changed=True,
session_id=session_id)
Expand All @@ -250,25 +342,22 @@ def remove_session(module):
session_id, e))


def get_consul_api(module):
return consul.Consul(host=module.params.get('host'),
port=module.params.get('port'),
scheme=module.params.get('scheme'),
verify=module.params.get('validate_certs'),
token=module.params.get('token'))


def test_dependencies(module):
if not python_consul_installed:
module.fail_json(msg="python-consul required for this module. "
"see https://python-consul.readthedocs.io/en/latest/#installation")
if not has_requests:
raise ImportError(
"requests required for this module. See https://pypi.org/project/requests/")


def main():
argument_spec = dict(
checks=dict(type='list', elements='str'),
delay=dict(type='int', default='15'),
behavior=dict(type='str', default='release', choices=['release', 'delete']),
behavior=dict(
type='str',
default='release',
choices=[
'release',
'delete']),
ttl=dict(type='int'),
host=dict(type='str', default='localhost'),
port=dict(type='int', default=8500),
Expand All @@ -277,7 +366,15 @@ def main():
id=dict(type='str'),
name=dict(type='str'),
node=dict(type='str'),
state=dict(type='str', default='present', choices=['absent', 'info', 'list', 'node', 'present']),
state=dict(
type='str',
default='present',
choices=[
'absent',
'info',
'list',
'node',
'present']),
datacenter=dict(type='str'),
token=dict(type='str', no_log=True),
)
Expand Down
14 changes: 13 additions & 1 deletion tests/integration/targets/consul/tasks/consul_session.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- name: list sessions
consul_session:
state: list
token: "{{ consul_management_token }}"
register: result

- assert:
Expand All @@ -17,6 +18,7 @@
consul_session:
state: present
name: testsession
token: "{{ consul_management_token }}"
register: result

- assert:
Expand All @@ -31,6 +33,7 @@
- name: list sessions after creation
consul_session:
state: list
token: "{{ consul_management_token }}"
register: result

- set_fact:
Expand All @@ -52,12 +55,13 @@
- name: ensure session was created
assert:
that:
- test_session_found|default(False)
- test_session_found|default(false)

- name: fetch info about a session
consul_session:
state: info
id: '{{ session_id }}'
token: "{{ consul_management_token }}"
register: result

- assert:
Expand All @@ -68,6 +72,7 @@
consul_session:
state: info
name: test
token: "{{ consul_management_token }}"
register: result
ignore_errors: true

Expand All @@ -80,6 +85,7 @@
state: info
id: '{{ session_id }}'
scheme: non_existent
token: "{{ consul_management_token }}"
register: result
ignore_errors: true

Expand All @@ -93,6 +99,7 @@
id: '{{ session_id }}'
port: 8501
scheme: https
token: "{{ consul_management_token }}"
register: result
ignore_errors: true

Expand All @@ -108,6 +115,7 @@
id: '{{ session_id }}'
port: 8501
scheme: https
token: "{{ consul_management_token }}"
validate_certs: false
register: result

Expand All @@ -122,6 +130,7 @@
id: '{{ session_id }}'
port: 8501
scheme: https
token: "{{ consul_management_token }}"
environment:
REQUESTS_CA_BUNDLE: '{{ remote_dir }}/cert.pem'
register: result
Expand All @@ -134,6 +143,7 @@
consul_session:
state: absent
id: '{{ session_id }}'
token: "{{ consul_management_token }}"
register: result

- assert:
Expand All @@ -143,6 +153,7 @@
- name: list sessions after deletion
consul_session:
state: list
token: "{{ consul_management_token }}"
register: result

- assert:
Expand All @@ -169,6 +180,7 @@
state: present
name: session-with-ttl
ttl: 180 # sec
token: "{{ consul_management_token }}"
register: result

- assert:
Expand Down
13 changes: 10 additions & 3 deletions tests/integration/targets/consul/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

- name: Install Consul and test
vars:
consul_version: 1.5.0
consul_uri: https://s3.amazonaws.com/ansible-ci-files/test/integration/targets/consul/consul_{{ consul_version }}_{{ ansible_system | lower }}_{{ consul_arch }}.zip
consul_version: 1.13.2
consul_uri: https://releases.hashicorp.com/consul/{{ consul_version }}/consul_{{ consul_version }}_{{ ansible_system | lower }}_{{ consul_arch }}.zip
consul_cmd: '{{ remote_tmp_dir }}/consul'
block:
- name: Install requests<2.20 (CentOS/RHEL 6)
Expand Down Expand Up @@ -76,8 +76,15 @@
dest: '{{ remote_tmp_dir }}/consul_config.hcl'
- name: Start Consul (dev mode enabled)
shell: nohup {{ consul_cmd }} agent -dev -config-file {{ remote_tmp_dir }}/consul_config.hcl </dev/null >/dev/null 2>&1 &
- name: Bootstrap ACL
command: '{{ consul_cmd }} acl bootstrap --format=json'
register: consul_bootstrap_result_string
- set_fact:
consul_management_token: '{{ consul_bootstrap_json_result["SecretID"] }}'
vars:
consul_bootstrap_json_result: '{{ consul_bootstrap_result_string.stdout | from_json }}'
- name: Create some data
command: '{{ consul_cmd }} kv put data/value{{ item }} foo{{ item }}'
command: '{{ consul_cmd }} kv put -token={{consul_management_token}} data/value{{ item }} foo{{ item }}'
loop:
- 1
- 2
Expand Down
Loading