| 
									
										
										
										
											2021-10-18 19:44:29 +00:00
										 |  |  | import sure  # noqa # pylint: disable=unused-import | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | import boto3 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-22 14:41:34 -01:00
										 |  |  | from moto import mock_iot | 
					
						
							| 
									
										
										
										
											2022-08-13 09:49:43 +00:00
										 |  |  | from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | from botocore.exceptions import ClientError | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-23 09:01:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-04 04:11:17 -04:00
										 |  |  | @mock_iot | 
					
						
							|  |  |  | def test_endpoints(): | 
					
						
							|  |  |  |     region_name = "ap-northeast-1" | 
					
						
							|  |  |  |     client = boto3.client("iot", region_name=region_name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # iot:Data | 
					
						
							|  |  |  |     endpoint = client.describe_endpoint(endpointType="iot:Data") | 
					
						
							|  |  |  |     endpoint.should.have.key("endpointAddress").which.should_not.contain("ats") | 
					
						
							|  |  |  |     endpoint.should.have.key("endpointAddress").which.should.contain( | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |         f"iot.{region_name}.amazonaws.com" | 
					
						
							| 
									
										
										
										
											2020-09-04 04:11:17 -04:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # iot:Data-ATS | 
					
						
							|  |  |  |     endpoint = client.describe_endpoint(endpointType="iot:Data-ATS") | 
					
						
							|  |  |  |     endpoint.should.have.key("endpointAddress").which.should.contain( | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |         f"ats.iot.{region_name}.amazonaws.com" | 
					
						
							| 
									
										
										
										
											2020-09-04 04:11:17 -04:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # iot:Data-ATS | 
					
						
							|  |  |  |     endpoint = client.describe_endpoint(endpointType="iot:CredentialProvider") | 
					
						
							|  |  |  |     endpoint.should.have.key("endpointAddress").which.should.contain( | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |         f"credentials.iot.{region_name}.amazonaws.com" | 
					
						
							| 
									
										
										
										
											2020-09-04 04:11:17 -04:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # iot:Data-ATS | 
					
						
							|  |  |  |     endpoint = client.describe_endpoint(endpointType="iot:Jobs") | 
					
						
							|  |  |  |     endpoint.should.have.key("endpointAddress").which.should.contain( | 
					
						
							| 
									
										
										
										
											2022-11-17 21:41:08 -01:00
										 |  |  |         f"jobs.iot.{region_name}.amazonaws.com" | 
					
						
							| 
									
										
										
										
											2020-09-04 04:11:17 -04:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # raise InvalidRequestException | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         client.describe_endpoint(endpointType="iot:Abc") | 
					
						
							|  |  |  |     except client.exceptions.InvalidRequestException as exc: | 
					
						
							|  |  |  |         error_code = exc.response["Error"]["Code"] | 
					
						
							|  |  |  |         error_code.should.equal("InvalidRequestException") | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         raise Exception("Should have raised error") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | @mock_iot | 
					
						
							|  |  |  | def test_principal_policy(): | 
					
						
							| 
									
										
										
										
											2018-10-25 20:34:53 +09:00
										 |  |  |     client = boto3.client("iot", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     policy_name = "my-policy" | 
					
						
							|  |  |  |     doc = "{}" | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  |     client.create_policy(policyName=policy_name, policyDocument=doc) | 
					
						
							|  |  |  |     cert = client.create_keys_and_certificate(setAsActive=True) | 
					
						
							| 
									
										
										
										
											2018-10-25 20:34:53 +09:00
										 |  |  |     cert_arn = cert["certificateArn"] | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     client.attach_policy(policyName=policy_name, target=cert_arn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = client.list_principal_policies(principal=cert_arn) | 
					
						
							| 
									
										
										
										
											2018-10-28 17:13:17 +09:00
										 |  |  |     res.should.have.key("policies").which.should.have.length_of(1) | 
					
						
							|  |  |  |     for policy in res["policies"]: | 
					
						
							|  |  |  |         policy.should.have.key("policyName").which.should_not.be.none | 
					
						
							|  |  |  |         policy.should.have.key("policyArn").which.should_not.be.none | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # do nothing if policy have already attached to certificate | 
					
						
							|  |  |  |     client.attach_policy(policyName=policy_name, target=cert_arn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = client.list_principal_policies(principal=cert_arn) | 
					
						
							| 
									
										
										
										
											2018-10-25 20:34:53 +09:00
										 |  |  |     res.should.have.key("policies").which.should.have.length_of(1) | 
					
						
							|  |  |  |     for policy in res["policies"]: | 
					
						
							|  |  |  |         policy.should.have.key("policyName").which.should_not.be.none | 
					
						
							|  |  |  |         policy.should.have.key("policyArn").which.should_not.be.none | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res = client.list_policy_principals(policyName=policy_name) | 
					
						
							| 
									
										
										
										
											2022-04-18 20:44:56 +00:00
										 |  |  |     res.should.have.key("principals").length_of(1) | 
					
						
							|  |  |  |     res["principals"][0].should.match(f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:cert/") | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     client.detach_policy(policyName=policy_name, target=cert_arn) | 
					
						
							|  |  |  |     res = client.list_principal_policies(principal=cert_arn) | 
					
						
							| 
									
										
										
										
											2018-10-25 20:34:53 +09:00
										 |  |  |     res.should.have.key("policies").which.should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  |     res = client.list_policy_principals(policyName=policy_name) | 
					
						
							| 
									
										
										
										
											2018-10-25 20:34:53 +09:00
										 |  |  |     res.should.have.key("principals").which.should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2020-10-06 07:54:49 +02:00
										 |  |  |     with pytest.raises(ClientError) as e: | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  |         client.detach_policy(policyName=policy_name, target=cert_arn) | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     e.value.response["Error"]["Code"].should.equal("ResourceNotFoundException") | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_iot | 
					
						
							|  |  |  | def test_principal_policy_deprecated(): | 
					
						
							| 
									
										
										
										
											2017-11-10 18:44:02 +09:00
										 |  |  |     client = boto3.client("iot", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     policy_name = "my-policy" | 
					
						
							|  |  |  |     doc = "{}" | 
					
						
							| 
									
										
										
										
											2022-04-18 20:44:56 +00:00
										 |  |  |     client.create_policy(policyName=policy_name, policyDocument=doc) | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  |     cert = client.create_keys_and_certificate(setAsActive=True) | 
					
						
							| 
									
										
										
										
											2017-11-10 18:44:02 +09:00
										 |  |  |     cert_arn = cert["certificateArn"] | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     client.attach_principal_policy(policyName=policy_name, principal=cert_arn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = client.list_principal_policies(principal=cert_arn) | 
					
						
							| 
									
										
										
										
											2022-04-18 20:44:56 +00:00
										 |  |  |     res.should.have.key("policies").length_of(1) | 
					
						
							|  |  |  |     res["policies"][0].should.have.key("policyName").equal("my-policy") | 
					
						
							|  |  |  |     res["policies"][0].should.have.key("policyArn").equal( | 
					
						
							|  |  |  |         f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:policy/my-policy" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res = client.list_policy_principals(policyName=policy_name) | 
					
						
							| 
									
										
										
										
											2022-04-18 20:44:56 +00:00
										 |  |  |     res.should.have.key("principals").length_of(1) | 
					
						
							|  |  |  |     res["principals"][0].should.match(f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:cert/") | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     client.detach_principal_policy(policyName=policy_name, principal=cert_arn) | 
					
						
							|  |  |  |     res = client.list_principal_policies(principal=cert_arn) | 
					
						
							| 
									
										
										
										
											2017-11-10 18:44:02 +09:00
										 |  |  |     res.should.have.key("policies").which.should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  |     res = client.list_policy_principals(policyName=policy_name) | 
					
						
							| 
									
										
										
										
											2017-11-10 18:44:02 +09:00
										 |  |  |     res.should.have.key("principals").which.should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_iot | 
					
						
							|  |  |  | def test_principal_thing(): | 
					
						
							| 
									
										
										
										
											2017-11-10 18:44:02 +09:00
										 |  |  |     client = boto3.client("iot", region_name="ap-northeast-1") | 
					
						
							|  |  |  |     thing_name = "my-thing" | 
					
						
							| 
									
										
										
										
											2021-10-18 19:44:29 +00:00
										 |  |  |     client.create_thing(thingName=thing_name) | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  |     cert = client.create_keys_and_certificate(setAsActive=True) | 
					
						
							| 
									
										
										
										
											2017-11-10 18:44:02 +09:00
										 |  |  |     cert_arn = cert["certificateArn"] | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     client.attach_thing_principal(thingName=thing_name, principal=cert_arn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = client.list_principal_things(principal=cert_arn) | 
					
						
							| 
									
										
										
										
											2017-11-10 18:44:02 +09:00
										 |  |  |     res.should.have.key("things").which.should.have.length_of(1) | 
					
						
							| 
									
										
										
										
											2021-04-19 21:35:09 +09:00
										 |  |  |     res["things"][0].should.equal(thing_name) | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  |     res = client.list_thing_principals(thingName=thing_name) | 
					
						
							| 
									
										
										
										
											2022-04-18 20:44:56 +00:00
										 |  |  |     res.should.have.key("principals").length_of(1) | 
					
						
							|  |  |  |     res["principals"][0].should.match(f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:cert/") | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     client.detach_thing_principal(thingName=thing_name, principal=cert_arn) | 
					
						
							|  |  |  |     res = client.list_principal_things(principal=cert_arn) | 
					
						
							| 
									
										
										
										
											2017-11-10 18:44:02 +09:00
										 |  |  |     res.should.have.key("things").which.should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2019-05-28 08:56:49 +02:00
										 |  |  |     res = client.list_thing_principals(thingName=thing_name) | 
					
						
							| 
									
										
										
										
											2017-11-10 18:44:02 +09:00
										 |  |  |     res.should.have.key("principals").which.should.have.length_of(0) | 
					
						
							| 
									
										
										
										
											2018-01-04 18:59:37 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     with pytest.raises(ClientError) as e: | 
					
						
							| 
									
										
										
										
											2020-04-21 14:43:04 +09:00
										 |  |  |         client.list_thing_principals(thingName="xxx") | 
					
						
							| 
									
										
										
										
											2020-04-21 14:11:53 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 08:04:09 +02:00
										 |  |  |     e.value.response["Error"]["Code"].should.equal("ResourceNotFoundException") | 
					
						
							|  |  |  |     e.value.response["Error"]["Message"].should.equal( | 
					
						
							| 
									
										
										
										
											2020-04-21 14:11:53 +09:00
										 |  |  |         "Failed to list principals for thing xxx because the thing does not exist in your account" | 
					
						
							|  |  |  |     ) |