|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -import configparser |
3 | 2 | import json |
4 | 3 | import os |
5 | 4 | import shutil |
@@ -856,205 +855,6 @@ def test_multi_vault_password_ask(self, private_data_dir, job, mock_me): |
856 | 855 | assert '--vault-id dev@prompt' in ' '.join(args) |
857 | 856 | assert '--vault-id prod@prompt' in ' '.join(args) |
858 | 857 |
|
859 | | - @pytest.mark.parametrize("verify", (True, False)) |
860 | | - def test_k8s_credential(self, job, private_data_dir, verify, mock_me): |
861 | | - k8s = CredentialType.defaults['kubernetes_bearer_token']() |
862 | | - inputs = { |
863 | | - 'host': 'https://example.org/', |
864 | | - 'bearer_token': 'token123', |
865 | | - } |
866 | | - if verify: |
867 | | - inputs['verify_ssl'] = True |
868 | | - inputs['ssl_ca_cert'] = 'CERTDATA' |
869 | | - credential = Credential( |
870 | | - pk=1, |
871 | | - credential_type=k8s, |
872 | | - inputs=inputs, |
873 | | - ) |
874 | | - credential.inputs['bearer_token'] = encrypt_field(credential, 'bearer_token') |
875 | | - job.credentials.add(credential) |
876 | | - |
877 | | - env = {} |
878 | | - safe_env = {} |
879 | | - credential.credential_type.inject_credential(credential, env, safe_env, [], private_data_dir) |
880 | | - |
881 | | - assert env['K8S_AUTH_HOST'] == 'https://example.org/' |
882 | | - assert env['K8S_AUTH_API_KEY'] == 'token123' |
883 | | - |
884 | | - if verify: |
885 | | - assert env['K8S_AUTH_VERIFY_SSL'] == 'True' |
886 | | - local_path = to_host_path(env['K8S_AUTH_SSL_CA_CERT'], private_data_dir) |
887 | | - with open(local_path, 'r') as f: |
888 | | - cert = f.read() |
889 | | - assert cert == 'CERTDATA' |
890 | | - else: |
891 | | - assert env['K8S_AUTH_VERIFY_SSL'] == 'False' |
892 | | - assert 'K8S_AUTH_SSL_CA_CERT' not in env |
893 | | - |
894 | | - assert safe_env['K8S_AUTH_API_KEY'] == HIDDEN_PASSWORD |
895 | | - |
896 | | - def test_aws_cloud_credential(self, job, private_data_dir, mock_me): |
897 | | - aws = CredentialType.defaults['aws']() |
898 | | - credential = Credential(pk=1, credential_type=aws, inputs={'username': 'bob', 'password': 'secret'}) |
899 | | - credential.inputs['password'] = encrypt_field(credential, 'password') |
900 | | - job.credentials.add(credential) |
901 | | - |
902 | | - env = {} |
903 | | - safe_env = {} |
904 | | - credential.credential_type.inject_credential(credential, env, safe_env, [], private_data_dir) |
905 | | - |
906 | | - assert env['AWS_ACCESS_KEY_ID'] == 'bob' |
907 | | - assert env['AWS_SECRET_ACCESS_KEY'] == 'secret' |
908 | | - assert 'AWS_SECURITY_TOKEN' not in env |
909 | | - assert safe_env['AWS_SECRET_ACCESS_KEY'] == HIDDEN_PASSWORD |
910 | | - |
911 | | - def test_aws_cloud_credential_with_sts_token(self, private_data_dir, job, mock_me): |
912 | | - aws = CredentialType.defaults['aws']() |
913 | | - credential = Credential(pk=1, credential_type=aws, inputs={'username': 'bob', 'password': 'secret', 'security_token': 'token'}) |
914 | | - for key in ('password', 'security_token'): |
915 | | - credential.inputs[key] = encrypt_field(credential, key) |
916 | | - job.credentials.add(credential) |
917 | | - |
918 | | - env = {} |
919 | | - safe_env = {} |
920 | | - credential.credential_type.inject_credential(credential, env, safe_env, [], private_data_dir) |
921 | | - |
922 | | - assert env['AWS_ACCESS_KEY_ID'] == 'bob' |
923 | | - assert env['AWS_SECRET_ACCESS_KEY'] == 'secret' |
924 | | - assert env['AWS_SECURITY_TOKEN'] == 'token' |
925 | | - assert safe_env['AWS_SECRET_ACCESS_KEY'] == HIDDEN_PASSWORD |
926 | | - |
927 | | - @pytest.mark.parametrize("cred_env_var", ['GCE_CREDENTIALS_FILE_PATH', 'GOOGLE_APPLICATION_CREDENTIALS']) |
928 | | - def test_gce_credentials(self, cred_env_var, private_data_dir, job, mock_me): |
929 | | - gce = CredentialType.defaults['gce']() |
930 | | - credential = Credential(pk=1, credential_type=gce, inputs={'username': 'bob', 'project': 'some-project', 'ssh_key_data': self.EXAMPLE_PRIVATE_KEY}) |
931 | | - credential.inputs['ssh_key_data'] = encrypt_field(credential, 'ssh_key_data') |
932 | | - job.credentials.add(credential) |
933 | | - |
934 | | - env = {} |
935 | | - safe_env = {} |
936 | | - credential.credential_type.inject_credential(credential, env, safe_env, [], private_data_dir) |
937 | | - runner_path = env[cred_env_var] |
938 | | - local_path = to_host_path(runner_path, private_data_dir) |
939 | | - with open(local_path, 'rb') as f_host: |
940 | | - json_data = json.load(f_host) |
941 | | - assert json_data['type'] == 'service_account' |
942 | | - assert json_data['private_key'] == self.EXAMPLE_PRIVATE_KEY |
943 | | - assert json_data['client_email'] == 'bob' |
944 | | - assert json_data['project_id'] == 'some-project' |
945 | | - |
946 | | - def test_azure_rm_with_tenant(self, private_data_dir, job, mock_me): |
947 | | - azure = CredentialType.defaults['azure_rm']() |
948 | | - credential = Credential( |
949 | | - pk=1, credential_type=azure, inputs={'client': 'some-client', 'secret': 'some-secret', 'tenant': 'some-tenant', 'subscription': 'some-subscription'} |
950 | | - ) |
951 | | - credential.inputs['secret'] = encrypt_field(credential, 'secret') |
952 | | - job.credentials.add(credential) |
953 | | - |
954 | | - env = {} |
955 | | - safe_env = {} |
956 | | - credential.credential_type.inject_credential(credential, env, safe_env, [], private_data_dir) |
957 | | - |
958 | | - assert env['AZURE_CLIENT_ID'] == 'some-client' |
959 | | - assert env['AZURE_SECRET'] == 'some-secret' |
960 | | - assert env['AZURE_TENANT'] == 'some-tenant' |
961 | | - assert env['AZURE_SUBSCRIPTION_ID'] == 'some-subscription' |
962 | | - assert safe_env['AZURE_SECRET'] == HIDDEN_PASSWORD |
963 | | - |
964 | | - def test_azure_rm_with_password(self, private_data_dir, job, mock_me): |
965 | | - azure = CredentialType.defaults['azure_rm']() |
966 | | - credential = Credential( |
967 | | - pk=1, credential_type=azure, inputs={'subscription': 'some-subscription', 'username': 'bob', 'password': 'secret', 'cloud_environment': 'foobar'} |
968 | | - ) |
969 | | - credential.inputs['password'] = encrypt_field(credential, 'password') |
970 | | - job.credentials.add(credential) |
971 | | - |
972 | | - env = {} |
973 | | - safe_env = {} |
974 | | - credential.credential_type.inject_credential(credential, env, safe_env, [], private_data_dir) |
975 | | - |
976 | | - assert env['AZURE_SUBSCRIPTION_ID'] == 'some-subscription' |
977 | | - assert env['AZURE_AD_USER'] == 'bob' |
978 | | - assert env['AZURE_PASSWORD'] == 'secret' |
979 | | - assert env['AZURE_CLOUD_ENVIRONMENT'] == 'foobar' |
980 | | - assert safe_env['AZURE_PASSWORD'] == HIDDEN_PASSWORD |
981 | | - |
982 | | - def test_vmware_credentials(self, private_data_dir, job, mock_me): |
983 | | - vmware = CredentialType.defaults['vmware']() |
984 | | - credential = Credential(pk=1, credential_type=vmware, inputs={'username': 'bob', 'password': 'secret', 'host': 'https://example.org'}) |
985 | | - credential.inputs['password'] = encrypt_field(credential, 'password') |
986 | | - job.credentials.add(credential) |
987 | | - |
988 | | - env = {} |
989 | | - safe_env = {} |
990 | | - credential.credential_type.inject_credential(credential, env, safe_env, [], private_data_dir) |
991 | | - |
992 | | - assert env['VMWARE_USER'] == 'bob' |
993 | | - assert env['VMWARE_PASSWORD'] == 'secret' |
994 | | - assert env['VMWARE_HOST'] == 'https://example.org' |
995 | | - assert safe_env['VMWARE_PASSWORD'] == HIDDEN_PASSWORD |
996 | | - |
997 | | - def test_openstack_credentials(self, private_data_dir, job, mock_me): |
998 | | - task = jobs.RunJob() |
999 | | - task.instance = job |
1000 | | - openstack = CredentialType.defaults['openstack']() |
1001 | | - credential = Credential( |
1002 | | - pk=1, credential_type=openstack, inputs={'username': 'bob', 'password': 'secret', 'project': 'tenant-name', 'host': 'https://keystone.example.org'} |
1003 | | - ) |
1004 | | - credential.inputs['password'] = encrypt_field(credential, 'password') |
1005 | | - job.credentials.add(credential) |
1006 | | - |
1007 | | - private_data_files, ssh_key_data = task.build_private_data_files(job, private_data_dir) |
1008 | | - env = task.build_env(job, private_data_dir, private_data_files=private_data_files) |
1009 | | - credential.credential_type.inject_credential(credential, env, {}, [], private_data_dir) |
1010 | | - |
1011 | | - config_loc = to_host_path(env['OS_CLIENT_CONFIG_FILE'], private_data_dir) |
1012 | | - with open(config_loc, 'r') as f: |
1013 | | - shade_config = f.read() |
1014 | | - assert shade_config == '\n'.join( |
1015 | | - [ |
1016 | | - 'clouds:', |
1017 | | - ' devstack:', |
1018 | | - ' auth:', |
1019 | | - ' auth_url: https://keystone.example.org', |
1020 | | - ' password: secret', |
1021 | | - ' project_name: tenant-name', |
1022 | | - ' username: bob', |
1023 | | - ' verify: true', |
1024 | | - '', |
1025 | | - ] |
1026 | | - ) |
1027 | | - |
1028 | | - @pytest.mark.parametrize("ca_file", [None, '/path/to/some/file']) |
1029 | | - def test_rhv_credentials(self, private_data_dir, job, ca_file, mock_me): |
1030 | | - rhv = CredentialType.defaults['rhv']() |
1031 | | - inputs = { |
1032 | | - 'host': 'some-ovirt-host.example.org', |
1033 | | - 'username': 'bob', |
1034 | | - 'password': 'some-pass', |
1035 | | - } |
1036 | | - if ca_file: |
1037 | | - inputs['ca_file'] = ca_file |
1038 | | - credential = Credential(pk=1, credential_type=rhv, inputs=inputs) |
1039 | | - credential.inputs['password'] = encrypt_field(credential, 'password') |
1040 | | - job.credentials.add(credential) |
1041 | | - |
1042 | | - env = {} |
1043 | | - safe_env = {} |
1044 | | - credential.credential_type.inject_credential(credential, env, safe_env, [], private_data_dir) |
1045 | | - |
1046 | | - config = configparser.ConfigParser() |
1047 | | - host_path = to_host_path(env['OVIRT_INI_PATH'], private_data_dir) |
1048 | | - config.read(host_path) |
1049 | | - assert config.get('ovirt', 'ovirt_url') == 'some-ovirt-host.example.org' |
1050 | | - assert config.get('ovirt', 'ovirt_username') == 'bob' |
1051 | | - assert config.get('ovirt', 'ovirt_password') == 'some-pass' |
1052 | | - if ca_file: |
1053 | | - assert config.get('ovirt', 'ovirt_ca_file') == ca_file |
1054 | | - else: |
1055 | | - with pytest.raises(configparser.NoOptionError): |
1056 | | - config.get('ovirt', 'ovirt_ca_file') |
1057 | | - |
1058 | 858 | @pytest.mark.parametrize( |
1059 | 859 | 'authorize, expected_authorize', |
1060 | 860 | [ |
@@ -1089,68 +889,6 @@ def test_net_credentials(self, authorize, expected_authorize, job, private_data_ |
1089 | 889 | assert f.read() == self.EXAMPLE_PRIVATE_KEY |
1090 | 890 | assert safe_env['ANSIBLE_NET_PASSWORD'] == HIDDEN_PASSWORD |
1091 | 891 |
|
1092 | | - def test_terraform_cloud_credentials(self, job, private_data_dir, mock_me): |
1093 | | - terraform = CredentialType.defaults['terraform']() |
1094 | | - hcl_config = ''' |
1095 | | - backend "s3" { |
1096 | | - bucket = "s3_sample_bucket" |
1097 | | - key = "/tf_state/" |
1098 | | - region = "us-east-1" |
1099 | | - } |
1100 | | - ''' |
1101 | | - credential = Credential(pk=1, credential_type=terraform, inputs={'configuration': hcl_config}) |
1102 | | - credential.inputs['configuration'] = encrypt_field(credential, 'configuration') |
1103 | | - job.credentials.add(credential) |
1104 | | - |
1105 | | - env = {} |
1106 | | - safe_env = {} |
1107 | | - credential.credential_type.inject_credential(credential, env, safe_env, [], private_data_dir) |
1108 | | - |
1109 | | - local_path = to_host_path(env['TF_BACKEND_CONFIG_FILE'], private_data_dir) |
1110 | | - with open(local_path, 'r') as f: |
1111 | | - config = f.read() |
1112 | | - assert config == hcl_config |
1113 | | - |
1114 | | - def test_terraform_gcs_backend_credentials(self, job, private_data_dir, mock_me): |
1115 | | - terraform = CredentialType.defaults['terraform']() |
1116 | | - hcl_config = ''' |
1117 | | - backend "gcs" { |
1118 | | - bucket = "gce_storage" |
1119 | | - } |
1120 | | - ''' |
1121 | | - gce_backend_credentials = ''' |
1122 | | - { |
1123 | | - "type": "service_account", |
1124 | | - "project_id": "sample", |
1125 | | - "private_key_id": "eeeeeeeeeeeeeeeeeeeeeeeeeee", |
1126 | | - "private_key": "-----BEGIN PRIVATE KEY-----\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n-----END PRIVATE KEY-----\n", |
1127 | | - "client_email": "[email protected]", |
1128 | | - "client_id": "0123456789", |
1129 | | - "auth_uri": "https://accounts.google.com/o/oauth2/auth", |
1130 | | - "token_uri": "https://oauth2.googleapis.com/token", |
1131 | | - "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", |
1132 | | - "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/cloud-content-robot%40sample.iam.gserviceaccount.com", |
1133 | | - } |
1134 | | - ''' |
1135 | | - credential = Credential(pk=1, credential_type=terraform, inputs={'configuration': hcl_config, 'gce_credentials': gce_backend_credentials}) |
1136 | | - credential.inputs['configuration'] = encrypt_field(credential, 'configuration') |
1137 | | - credential.inputs['gce_credentials'] = encrypt_field(credential, 'gce_credentials') |
1138 | | - job.credentials.add(credential) |
1139 | | - |
1140 | | - env = {} |
1141 | | - safe_env = {} |
1142 | | - credential.credential_type.inject_credential(credential, env, safe_env, [], private_data_dir) |
1143 | | - |
1144 | | - local_path = to_host_path(env['TF_BACKEND_CONFIG_FILE'], private_data_dir) |
1145 | | - with open(local_path, 'r') as f: |
1146 | | - config = f.read() |
1147 | | - assert config == hcl_config |
1148 | | - |
1149 | | - credentials_path = to_host_path(env['GOOGLE_BACKEND_CREDENTIALS'], private_data_dir) |
1150 | | - with open(credentials_path, 'r') as f: |
1151 | | - credentials = f.read() |
1152 | | - assert credentials == gce_backend_credentials |
1153 | | - |
1154 | 892 | def test_multi_cloud(self, private_data_dir, mock_me): |
1155 | 893 | gce = CredentialType.defaults['gce']() |
1156 | 894 | gce_credential = Credential(pk=1, credential_type=gce, inputs={'username': 'bob', 'project': 'some-project', 'ssh_key_data': self.EXAMPLE_PRIVATE_KEY}) |
|
0 commit comments