|
18 | 18 | "# This Python code is compatible with AWS OpenSearch versions 2.9 and higher.\n",
|
19 | 19 | "class AIConnectorHelper:\n",
|
20 | 20 | " \n",
|
21 |
| - " def __init__(self, region, opensearch_domain_name, opensearch_domain_username, opensearch_domain_password, aws_user_name):\n", |
| 21 | + " def __init__(self, region, opensearch_domain_name, opensearch_domain_username, opensearch_domain_password, aws_user_name, aws_role_name):\n", |
22 | 22 | " self.region = region\n",
|
23 | 23 | " self.opensearch_domain_url, self.opensearch_domain_arn = AIConnectorHelper.get_opensearch_domain_info(region, opensearch_domain_name)\n",
|
24 | 24 | " self.opensearch_domain_username = opensearch_domain_username\n",
|
25 | 25 | " self.opensearch_domain_opensearch_domain_password = opensearch_domain_password\n",
|
26 | 26 | " self.aws_user_name = aws_user_name\n",
|
| 27 | + " self.aws_role_name = aws_role_name\n", |
27 | 28 | " \n",
|
28 | 29 | " @staticmethod \n",
|
29 | 30 | " def get_opensearch_domain_info(region, domain_name):\n",
|
|
46 | 47 | " return None, None\n",
|
47 | 48 | " \n",
|
48 | 49 | " def get_user_arn(self, username):\n",
|
| 50 | + " if not username:\n", |
| 51 | + " return None\n", |
49 | 52 | " # Create a boto3 client for IAM\n",
|
50 | 53 | " iam_client = boto3.client('iam')\n",
|
51 | 54 | "\n",
|
|
172 | 175 | " return None\n",
|
173 | 176 | "\n",
|
174 | 177 | " def get_role_arn(self, role_name):\n",
|
| 178 | + " if not role_name:\n", |
| 179 | + " return None\n", |
175 | 180 | " iam_client = boto3.client('iam')\n",
|
176 | 181 | " try:\n",
|
177 | 182 | " response = iam_client.get_role(RoleName=role_name)\n",
|
|
374 | 379 | " \"Statement\": [\n",
|
375 | 380 | " {\n",
|
376 | 381 | " \"Action\": [\n",
|
377 |
| - " \"secretsmanager:GetSecretValue\"\n", |
| 382 | + " \"secretsmanager:GetSecretValue\",\n", |
378 | 383 | " \"secretsmanager:DescribeSecret\"\n",
|
379 | 384 | " ],\n",
|
380 | 385 | " \"Effect\": \"Allow\",\n",
|
|
395 | 400 | " # Step 3: Configure IAM role in OpenSearch\n",
|
396 | 401 | " # 3.1 Create IAM role for Signing create connector request\n",
|
397 | 402 | " user_arn = self.get_user_arn(self.aws_user_name)\n",
|
| 403 | + " role_arn = self.get_role_arn(self.aws_role_name)\n", |
| 404 | + " statements = []\n", |
| 405 | + " if user_arn:\n", |
| 406 | + " statements.append({\n", |
| 407 | + " \"Effect\": \"Allow\",\n", |
| 408 | + " \"Principal\": {\n", |
| 409 | + " \"AWS\": user_arn\n", |
| 410 | + " },\n", |
| 411 | + " \"Action\": \"sts:AssumeRole\"\n", |
| 412 | + " })\n", |
| 413 | + " if role_arn:\n", |
| 414 | + " statements.append({\n", |
| 415 | + " \"Effect\": \"Allow\",\n", |
| 416 | + " \"Principal\": {\n", |
| 417 | + " \"AWS\": role_arn\n", |
| 418 | + " },\n", |
| 419 | + " \"Action\": \"sts:AssumeRole\"\n", |
| 420 | + " })\n", |
398 | 421 | " trust_policy = {\n",
|
399 | 422 | " \"Version\": \"2012-10-17\",\n",
|
400 |
| - " \"Statement\": [\n", |
401 |
| - " {\n", |
402 |
| - " \"Effect\": \"Allow\",\n", |
403 |
| - " \"Principal\": {\n", |
404 |
| - " \"AWS\": user_arn\n", |
405 |
| - " },\n", |
406 |
| - " \"Action\": \"sts:AssumeRole\"\n", |
407 |
| - " }\n", |
408 |
| - " ]\n", |
| 423 | + " \"Statement\": statements\n", |
409 | 424 | " }\n",
|
410 | 425 | "\n",
|
411 | 426 | " inline_policy = {\n",
|
|
486 | 501 | " # Step 2: Configure IAM role in OpenSearch\n",
|
487 | 502 | " # 2.1 Create IAM role for Signing create connector request\n",
|
488 | 503 | " user_arn = self.get_user_arn(self.aws_user_name)\n",
|
| 504 | + " role_arn = self.get_role_arn(self.aws_role_name)\n", |
| 505 | + " statements = []\n", |
| 506 | + " if user_arn:\n", |
| 507 | + " statements.append({\n", |
| 508 | + " \"Effect\": \"Allow\",\n", |
| 509 | + " \"Principal\": {\n", |
| 510 | + " \"AWS\": user_arn\n", |
| 511 | + " },\n", |
| 512 | + " \"Action\": \"sts:AssumeRole\"\n", |
| 513 | + " })\n", |
| 514 | + " if role_arn:\n", |
| 515 | + " statements.append({\n", |
| 516 | + " \"Effect\": \"Allow\",\n", |
| 517 | + " \"Principal\": {\n", |
| 518 | + " \"AWS\": role_arn\n", |
| 519 | + " },\n", |
| 520 | + " \"Action\": \"sts:AssumeRole\"\n", |
| 521 | + " })\n", |
489 | 522 | " trust_policy = {\n",
|
490 | 523 | " \"Version\": \"2012-10-17\",\n",
|
491 |
| - " \"Statement\": [\n", |
492 |
| - " {\n", |
493 |
| - " \"Effect\": \"Allow\",\n", |
494 |
| - " \"Principal\": {\n", |
495 |
| - " \"AWS\": user_arn\n", |
496 |
| - " },\n", |
497 |
| - " \"Action\": \"sts:AssumeRole\"\n", |
498 |
| - " }\n", |
499 |
| - " ]\n", |
| 524 | + " \"Statement\": statements\n", |
500 | 525 | " }\n",
|
501 | 526 | "\n",
|
502 | 527 | " inline_policy = {\n",
|
|
571 | 596 | "opensearch_domain_password = '...' # set your domain password\n",
|
572 | 597 | "\n",
|
573 | 598 | "aws_user_name = '...' # set your AWS IAM user name, not IAM user ARN. \n",
|
574 |
| - " # To avoid permission issue and quick start, you can use user whith AdministratorAccess policy\n", |
| 599 | + " # To avoid permission issue and quick start, you can use user with AdministratorAccess policy\n", |
575 | 600 | " # Configure this user's access key and secret key in ~/.aws/credential \n",
|
576 | 601 | " # You can configure ~/.aws/credential as:\n",
|
577 | 602 | "'''\n",
|
578 | 603 | "[default]\n",
|
579 | 604 | "AWS_ACCESS_KEY_ID = YOUR_ACCESS_KEY_ID\n",
|
580 | 605 | "AWS_SECRET_ACCESS_KEY = YOUR_SECRET_ACCESS_KEY\n",
|
581 | 606 | "'''\n",
|
| 607 | + "aws_role_name = '...' # set your AWS IAM role name, not IAM role ARN.\n", |
| 608 | + " # To avoid permission issue and quick start, you can use role with AdministratorAccess policy\n", |
| 609 | + " # You can configure role temporary credential in ~/.aws/credential as:\n", |
| 610 | + "'''\n", |
| 611 | + "[default]\n", |
| 612 | + "AWS_ACCESS_KEY_ID = YOUR_ACCESS_KEY_ID\n", |
| 613 | + "AWS_SECRET_ACCESS_KEY = YOUR_SECRET_ACCESS_KEY\n", |
| 614 | + "AWS_SESSION_TOKEN = YOUR_AWS_SESSION_TOKEN\n", |
| 615 | + "'''\n", |
| 616 | + "# You must set either aws_user_name or aws_role_name. \n", |
| 617 | + "# You can set the one which you don't need as None. For example aws_role_name=None\n", |
582 | 618 | "\n",
|
583 | 619 | "helper = AIConnectorHelper(region, \n",
|
584 | 620 | " opensearch_domain_name, \n",
|
585 | 621 | " opensearch_domain_username, \n",
|
586 | 622 | " opensearch_domain_password, \n",
|
587 |
| - " aws_user_name)" |
| 623 | + " aws_user_name,\n", |
| 624 | + " aws_role_name)" |
588 | 625 | ]
|
589 | 626 | },
|
590 | 627 | {
|
|
0 commit comments