| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: iam_server_certificate_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Retrieve the information of a server certificate |
| 12 |
description: |
| 13 |
- Retrieve the attributes of a server certificate. |
| 14 |
author: |
| 15 |
- "Allen Sanabria (@linuxdynasty)" |
| 16 |
options: |
| 17 |
name: |
| 18 |
description: |
| 19 |
- The name of the server certificate you are retrieving attributes for. |
| 20 |
type: str |
| 21 |
extends_documentation_fragment: |
| 22 |
- amazon.aws.common.modules |
| 23 |
- amazon.aws.region.modules |
| 24 |
- amazon.aws.boto3 |
| 25 |
""" |
| 26 |
|
| 27 |
EXAMPLES = r""" |
| 28 |
- name: Retrieve server certificate |
| 29 |
community.aws.iam_server_certificate_info: |
| 30 |
name: production-cert |
| 31 |
register: server_cert |
| 32 |
|
| 33 |
- name: Fail if the server certificate name was not found |
| 34 |
community.aws.iam_server_certificate_info: |
| 35 |
name: production-cert |
| 36 |
register: server_cert |
| 37 |
failed_when: "{{ server_cert.results | length == 0 }}" |
| 38 |
""" |
| 39 |
|
| 40 |
RETURN = r""" |
| 41 |
server_certificate_id: |
| 42 |
description: The 21 character certificate id |
| 43 |
returned: success |
| 44 |
type: str |
| 45 |
sample: "ADWAJXWTZAXIPIMQHMJPO" |
| 46 |
certificate_body: |
| 47 |
description: The asn1der encoded PEM string |
| 48 |
returned: success |
| 49 |
type: str |
| 50 |
sample: "-----BEGIN CERTIFICATE-----\nbunch of random data\n-----END CERTIFICATE-----" |
| 51 |
server_certificate_name: |
| 52 |
description: The name of the server certificate |
| 53 |
returned: success |
| 54 |
type: str |
| 55 |
sample: "server-cert-name" |
| 56 |
arn: |
| 57 |
description: The Amazon resource name of the server certificate |
| 58 |
returned: success |
| 59 |
type: str |
| 60 |
sample: "arn:aws:iam::123456789012:server-certificate/server-cert-name" |
| 61 |
path: |
| 62 |
description: The path of the server certificate |
| 63 |
returned: success |
| 64 |
type: str |
| 65 |
sample: "/" |
| 66 |
expiration: |
| 67 |
description: The date and time this server certificate will expire, in ISO 8601 format. |
| 68 |
returned: success |
| 69 |
type: str |
| 70 |
sample: "2017-06-15T12:00:00+00:00" |
| 71 |
upload_date: |
| 72 |
description: The date and time this server certificate was uploaded, in ISO 8601 format. |
| 73 |
returned: success |
| 74 |
type: str |
| 75 |
sample: "2015-04-25T00:36:40+00:00" |
| 76 |
""" |
| 77 |
|
| 78 |
|
| 79 |
try: |
| 80 |
import botocore |
| 81 |
except ImportError: |
| 82 |
pass # Handled by AnsibleAWSModule |
| 83 |
|
| 84 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 85 |
|
| 86 |
|
| 87 |
def get_server_certs(iam, name=None): |
| 88 |
"""Retrieve the attributes of a server certificate if it exists or all certs. |
| 89 |
Args: |
| 90 |
iam (botocore.client.IAM): The boto3 iam instance. |
| 91 |
|
| 92 |
Kwargs: |
| 93 |
name (str): The name of the server certificate. |
| 94 |
|
| 95 |
Basic Usage: |
| 96 |
>>> import boto3 |
| 97 |
>>> iam = boto3.client('iam') |
| 98 |
>>> name = "server-cert-name" |
| 99 |
>>> results = get_server_certs(iam, name) |
| 100 |
{ |
| 101 |
"upload_date": "2015-04-25T00:36:40+00:00", |
| 102 |
"server_certificate_id": "ADWAJXWTZAXIPIMQHMJPO", |
| 103 |
"certificate_body": "-----BEGIN CERTIFICATE-----\nbunch of random data\n-----END CERTIFICATE-----", |
| 104 |
"server_certificate_name": "server-cert-name", |
| 105 |
"expiration": "2017-06-15T12:00:00+00:00", |
| 106 |
"path": "/", |
| 107 |
"arn": "arn:aws:iam::123456789012:server-certificate/server-cert-name" |
| 108 |
} |
| 109 |
""" |
| 110 |
results = dict() |
| 111 |
try: |
| 112 |
if name: |
| 113 |
server_certs = [iam.get_server_certificate(ServerCertificateName=name)["ServerCertificate"]] |
| 114 |
else: |
| 115 |
server_certs = iam.list_server_certificates()["ServerCertificateMetadataList"] |
| 116 |
|
| 117 |
for server_cert in server_certs: |
| 118 |
if not name: |
| 119 |
server_cert = iam.get_server_certificate(ServerCertificateName=server_cert["ServerCertificateName"])[ |
| 120 |
"ServerCertificate" |
| 121 |
] |
| 122 |
cert_md = server_cert["ServerCertificateMetadata"] |
| 123 |
results[cert_md["ServerCertificateName"]] = { |
| 124 |
"certificate_body": server_cert["CertificateBody"], |
| 125 |
"server_certificate_id": cert_md["ServerCertificateId"], |
| 126 |
"server_certificate_name": cert_md["ServerCertificateName"], |
| 127 |
"arn": cert_md["Arn"], |
| 128 |
"path": cert_md["Path"], |
| 129 |
"expiration": cert_md["Expiration"].isoformat(), |
| 130 |
"upload_date": cert_md["UploadDate"].isoformat(), |
| 131 |
} |
| 132 |
|
| 133 |
except botocore.exceptions.ClientError: |
| 134 |
pass |
| 135 |
|
| 136 |
return results |
| 137 |
|
| 138 |
|
| 139 |
def main(): |
| 140 |
argument_spec = dict( |
| 141 |
name=dict(type="str"), |
| 142 |
) |
| 143 |
|
| 144 |
module = AnsibleAWSModule( |
| 145 |
argument_spec=argument_spec, |
| 146 |
supports_check_mode=True, |
| 147 |
) |
| 148 |
|
| 149 |
try: |
| 150 |
iam = module.client("iam") |
| 151 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 152 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 153 |
|
| 154 |
cert_name = module.params.get("name") |
| 155 |
results = get_server_certs(iam, cert_name) |
| 156 |
module.exit_json(results=results) |
| 157 |
|
| 158 |
|
| 159 |
if __name__ == "__main__": |
| 160 |
main() |