From 2064fc3fc50234c3230043e45c6f43bba4b443a7 Mon Sep 17 00:00:00 2001
From: Tony <30942843+axc450@users.noreply.github.com>
Date: Wed, 11 Sep 2019 18:00:05 +0100
Subject: [PATCH] Added AWS SAML Federation Module (#55821)
* Added AWS SAML Federation Module
* iam_saml_federation: (integration tests) initial integration tests
---
lib/ansible/config/module_defaults.yml | 2 +
.../cloud/amazon/iam_saml_federation.py | 249 ++++++++++++++++++
.../targets/iam_saml_federation/aliases | 2 +
.../iam_saml_federation/files/example1.xml | 22 ++
.../iam_saml_federation/files/example2.xml | 22 ++
.../targets/iam_saml_federation/meta/main.yml | 3 +
.../iam_saml_federation/tasks/main.yml | 87 ++++++
7 files changed, 387 insertions(+)
create mode 100644 lib/ansible/modules/cloud/amazon/iam_saml_federation.py
create mode 100644 test/integration/targets/iam_saml_federation/aliases
create mode 100644 test/integration/targets/iam_saml_federation/files/example1.xml
create mode 100644 test/integration/targets/iam_saml_federation/files/example2.xml
create mode 100644 test/integration/targets/iam_saml_federation/meta/main.yml
create mode 100644 test/integration/targets/iam_saml_federation/tasks/main.yml
diff --git a/lib/ansible/config/module_defaults.yml b/lib/ansible/config/module_defaults.yml
index e82c38b2ff6..c17e8c8f9c3 100644
--- a/lib/ansible/config/module_defaults.yml
+++ b/lib/ansible/config/module_defaults.yml
@@ -406,6 +406,8 @@ groupings:
- aws
iam_server_certificate_facts:
- aws
+ iam_saml_federation:
+ - aws
iam_server_certificate_info:
- aws
iam_user:
diff --git a/lib/ansible/modules/cloud/amazon/iam_saml_federation.py b/lib/ansible/modules/cloud/amazon/iam_saml_federation.py
new file mode 100644
index 00000000000..ee3c720afbd
--- /dev/null
+++ b/lib/ansible/modules/cloud/amazon/iam_saml_federation.py
@@ -0,0 +1,249 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see .
+
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+ANSIBLE_METADATA = {'metadata_version': '1.1',
+ 'status': ['preview'],
+ 'supported_by': 'community'}
+
+DOCUMENTATION = '''
+---
+module: iam_saml_federation
+version_added: "2.10"
+short_description: Maintain IAM SAML federation configuration.
+requirements:
+ - boto3
+description:
+ - Provides a mechanism to manage AWS IAM SAML Identity Federation providers (create/update/delete metadata).
+options:
+ name:
+ description:
+ - The name of the provider to create.
+ required: true
+ type: str
+ saml_metadata_document:
+ description:
+ - The XML document generated by an identity provider (IdP) that supports SAML 2.0.
+ type: str
+ state:
+ description:
+ - Whether to create or delete identity provider. If 'present' is specified it will attempt to update the identity provider matching the name field.
+ default: present
+ choices: [ "present", "absent" ]
+ type: str
+extends_documentation_fragment:
+ - aws
+ - ec2
+author:
+ - Tony (@axc450)
+ - Aidan Rowe (@aidan-)
+'''
+
+EXAMPLES = '''
+# Note: These examples do not set authentication details, see the AWS Guide for details.
+# It is assumed that their matching environment variables are set.
+# Creates a new iam saml identity provider if not present
+- name: saml provider
+ iam_saml_federation:
+ name: example1
+ # the > below opens an indented block, so no escaping/quoting is needed when in the indentation level under this key
+ saml_metadata_document: >
+ ...
+
+
+
+
+
+
+ MIIDJjCCAg4CCQCiwst2XYH7fTANBgkqhkiG9w0BAQsFADBVMQswCQYDVQQGEwJYWDEVMBMGA1UEBwwMRGVmYXVsdCBDaXR5MRwwGgYDVQQKDBNEZWZhdWx0IENvbXBhbnkgTHRkMREwDwYDVQQDDAhleGFtcGxlMTAeFw0xOTA4MjIyMDM2NTFaFw0yMDA4MjEyMDM2NTFaMFUxCzAJBgNVBAYTAlhYMRUwEwYDVQQHDAxEZWZhdWx0IEN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DVQQHDAxEZWZhdWx0IENpdHkxHDAaBgNVBAoME0RlZmF1bHQgQ29tcGFueSBMdGQxETAPBgNVBAMMCGV4YW1wbGUxMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArLbBVE6E28bfvB/gUGjOmY2lxxxLZ9Fls4fOH9js/MhGG+hh4diyj/Kb7Coo6HehXMp93TXkYYbiKGAoykT6ULEACZnYi1V9XdUs619ibumi9pRSFygBrbyN+n9peMJxf4jvM1QS/DTPWxdkgeMkqb2SARJChd3azCHd0cdGwcsx1pTkYp34SL0gP79m6W8N3TIxyJmqi0Kc7mntPQUCVH/wFSyg59JXo8SUQDQNap/yd9UwLzxP9MhH8G3DBatwQj3ijYOPnAeUPbsw7GYiKQBh/SIH5DGzW4TNHo0PiQJqzymNp0mI0eKjRO98vfnsXkeQwotzeKVbkmJ63h3PHQIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQBvm+zYchto1NESDxCVDK96QKObklWrfiAgKDLb49Loox+pyWTvs2mu5DOgDe0rrgEDfxngbbupo9eSu5w7OPVfip8W9rsB8k6ak+P4G8MltqkYv5A0aXbka1da1NenbIXAC3/YbMjLnsidDWQiKYZ0i0HxjuhguW3lvOFd3Dzp2rNDydzA6ilSmBXFrAcKm0RHAfP4NGy3ECdU6SQ5OBSUcJprKADMODIykuds1qh0Gz8a0ukKKmp2yJvz9bIuC4+TRXKKZtgDZKPcN0MgtqYZJ2rttoFqkCWrNBCZSUgJEASUJ78NSC3Wy8WQr3NjZvQ86KG2/mcVQ3Lm1ci82Uue
+
+
+
+
+ urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified
+
+
+
diff --git a/test/integration/targets/iam_saml_federation/files/example2.xml b/test/integration/targets/iam_saml_federation/files/example2.xml
new file mode 100644
index 00000000000..76a86c7a761
--- /dev/null
+++ b/test/integration/targets/iam_saml_federation/files/example2.xml
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+ MIIDADCCAegCCQCgxBiDM2muazANB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BMGA1UEBwwMRGVmYXVsdCBDaXR5MRwwGgYDVQQKDBNEZWZhdWx0IENvbXBhbnkgTHRkMB4XDTE5MDgyMjIwMzY1OFoXDTIwMDgyMTIwMzY1OFowQjELMAkGA1UEBhMCWFgxFTATBgNVBAcMDERlZmF1bHQgQ2l0eTEcMBoGA1UECgwTRGVmYXVsdCBDb21wYW55IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMUJ3J1tzqoAgQwaJHx/MGl5yVTNpJLPfx8YCS0Z+RQWXIazZrssy/tpZcfgnek4+xvqrzRXR4nell31VTojIGItqR70lPhrsPES70SrN8egi+MLTZ4iddG5hjK4bn4wss88/3johi8/J85wc26/bkRz66lOvTaJ8k1pncQ3NekT9zZzWlW1LQk3uMbaPrVVocjFBEZyTsYUE9wZG+ggRBJlOMGEdhGsgPuR8Aj7OXO7X8/RolV8lB3GTzellX2GxiWnOhjnabSPBUUv5iVKcDOb2lIqxr5DScIvX1PcJSUCAGGLcd8wYK/lh3k+PFH9QNDLY6F5WHkoZq9LS46+8lkCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAWjX7E/BYAHaOKOXc5RAD9zwAaMxLMTSK5Cnq32TGIh1P4ap8jTNVaiCs9UJXHJpKwXUN+3DdVBIGMT17DzFwAeruZOzNBN0VJVl0yZ6dARgss4gpOBGvBD8blLidnVxEd5VRGldx5R5+I441ms6ASkohcHhGlF4WGbnabEZ/MtxhDIWUX2w4naOfFg6vOiPsE1C/ZXJeLDNP+dnjfueTN5DD38d+ND2mHweB7u0Qjpkd2K0TuCp0z4kXRuTgPzlfkPORNkgyU1hA3YClpT57aeUsHgO23sr/4d04jzI+hYeleGqjNM+3vDQYsOQyXx61/nANeF0Sp9ZIv3eJSTMXNw==
+
+
+
+
+ urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified
+
+
+
diff --git a/test/integration/targets/iam_saml_federation/meta/main.yml b/test/integration/targets/iam_saml_federation/meta/main.yml
new file mode 100644
index 00000000000..1f64f1169a9
--- /dev/null
+++ b/test/integration/targets/iam_saml_federation/meta/main.yml
@@ -0,0 +1,3 @@
+dependencies:
+ - prepare_tests
+ - setup_ec2
diff --git a/test/integration/targets/iam_saml_federation/tasks/main.yml b/test/integration/targets/iam_saml_federation/tasks/main.yml
new file mode 100644
index 00000000000..2ee7daef97e
--- /dev/null
+++ b/test/integration/targets/iam_saml_federation/tasks/main.yml
@@ -0,0 +1,87 @@
+- module_defaults:
+ group/aws:
+ region: "{{ aws_region }}"
+ aws_access_key: "{{ aws_access_key }}"
+ aws_secret_key: "{{ aws_secret_key }}"
+ security_token: "{{ security_token | default(omit) }}"
+ block:
+ # ============================================================
+ # TESTS
+ - name: Create the identity provider
+ iam_saml_federation:
+ name: '{{ resource_prefix }}-saml'
+ state: present
+ saml_metadata_document: '{{ lookup("file", "example1.xml") }}'
+ register: create_result
+
+ - name: assert idp created
+ assert:
+ that:
+ - create_result is changed
+
+ - name: Test that nothing changes when we retry
+ iam_saml_federation:
+ name: '{{ resource_prefix }}-saml'
+ state: present
+ saml_metadata_document: '{{ lookup("file", "example1.xml") }}'
+ register: create_result
+
+ - name: assert the idp doesn't change when we retry
+ assert:
+ that:
+ - create_result is not changed
+
+ - name: Change the identity provider
+ iam_saml_federation:
+ name: '{{ resource_prefix }}-saml'
+ state: present
+ saml_metadata_document: '{{ lookup("file", "example2.xml") }}'
+ register: change_result
+
+ - name: assert idp created
+ assert:
+ that:
+ - change_result is changed
+
+ - name: Test that nothing changes when we retry
+ iam_saml_federation:
+ name: '{{ resource_prefix }}-saml'
+ state: present
+ saml_metadata_document: '{{ lookup("file", "example2.xml") }}'
+ register: change_result
+
+ - name: assert the idp doesn't change when we retry
+ assert:
+ that:
+ - change_result is not changed
+
+ - name: Delete the identity provider
+ iam_saml_federation:
+ name: '{{ resource_prefix }}-saml'
+ state: absent
+ register: destroy_result
+
+ - name: assert deleted
+ assert:
+ that:
+ - destroy_result is changed
+
+ - name: Attempt to re-delete the identity provider
+ iam_saml_federation:
+ name: '{{ resource_prefix }}-saml'
+ state: absent
+ register: destroy_result
+
+ - name: assert deleted
+ assert:
+ that:
+ - destroy_result is not changed
+
+ always:
+ # ============================================================
+ # CLEAN-UP
+ - name: finish off by deleting the identity provider
+ iam_saml_federation:
+ name: '{{ resource_prefix }}-saml'
+ state: absent
+ register: destroy_result