Răsfoiți Sursa

cloudformation push

sunguroku 3 ani în urmă
părinte
comite
6de19dfc3e

+ 5 - 1
api/server/handlers/project_integration/preflight_check_aws.go

@@ -51,7 +51,11 @@ func (p *CreatePreflightCheckAWSHandler) ServeHTTP(w http.ResponseWriter, r *htt
 
 	if err != nil {
 		e := fmt.Errorf("preflight check failed: %w", err)
-		p.HandleAPIError(w, r, apierrors.NewErrInternal(e))
+		p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+			e,
+			http.StatusNotFound,
+		))
+
 		return
 	}
 	

+ 36 - 24
dashboard/src/components/CloudFormationForm.tsx

@@ -28,35 +28,46 @@ const CloudFormationForm: React.FC<Props> = ({
   const [AWSAccountID, setAWSAccountID] = useState("");
   const [grantPermissionsError, setGrantPermissionsError] = useState("");
   const [roleStatus, setRoleStatus] = useState("");
+  const { currentProject } = useContext(Context);
+
+  const getExternalId = () => {
+    let externalId = localStorage.getItem(AWSAccountID)
+    console.log(externalId)
+    if (!externalId) {
+      externalId = uuidv4() 
+      localStorage.setItem(AWSAccountID, externalId);
+    }
+
+    return externalId
+  }
 
   const checkIfRoleExists = () => {
+    let externalId = getExternalId();
     let targetARN = `arn:aws:iam::${AWSAccountID}:role/porter-role`
     setRoleStatus("loading");
-    // api
-    //   .preflightCheckAWS(
-    //     "<token>",
-    //     {
-    //       target_arn: targetARN,
-    //       external_id: externalID,
-    //     },
-    //     {
-    //       id: currentProject.id,
-    //     }
-    //   )
-    //   .then(({ data }) => {
-    //     setRoleStatus("successful");
-    //     proceed();
-    //   })
-    //   .catch((err) => {
-    //     console.error(err);
-    //     setCreateStatus("Error creating credentials");
-    //   });
-      setRoleStatus("successful");
-      proceed(targetARN);
+    api
+      .preflightCheckAWS(
+        "<token>",
+        {
+          target_arn: targetARN,
+          external_id: externalId,
+        },
+        {
+          id: currentProject.id,
+        }
+      )
+      .then(({ data }) => {
+        setRoleStatus("successful");
+        proceed(targetARN);
+      })
+      .catch((err) => {
+        console.log(err);
+        setRoleStatus("Role does not exist in the AWS account.");
+      });
   };
 
   const directToCloudFormation = () => {
-    let externalId = uuidv4();
+    let externalId = getExternalId();
     window.open(
       `https://console.aws.amazon.com/cloudformation/home?
       #/stacks/create/review?templateURL=https://porter-role.s3.us-east-2.amazonaws.com/cloudformation-policy.json&stackName=PorterRole&param_ExternalIdParameter=${externalId}`
@@ -90,7 +101,6 @@ const CloudFormationForm: React.FC<Props> = ({
                 </i>
               </Flex>
             }
-            type="number"
             value={AWSAccountID}
             setValue={(e) => {
               setGrantPermissionsError("");
@@ -117,7 +127,9 @@ const CloudFormationForm: React.FC<Props> = ({
         </Fieldset>
         <Spacer y={1} />
         <SaveButton
-          onClick={checkIfRoleExists}
+          onClick={() => {
+            checkIfRoleExists()
+          }}
           status={roleStatus}
           statusPosition="right"
           clearPosition

+ 1 - 0
dashboard/src/components/porter/Input.tsx

@@ -96,6 +96,7 @@ const StyledInput = styled.input<{
   outline: none;
   border-radius: 5px;
   background: #26292e;
+
   border: 1px solid ${props => props.hasError ? "#ff3b62" : "#494b4f"};
   :hover {
     border: 1px solid ${props => props.hasError ? "#ff3b62" : "#7a7b80"};

+ 4 - 3
zarf/cloudformation-policy.json

@@ -1,7 +1,7 @@
 {
     "AWSTemplateFormatVersion": "2010-09-09",
     "Resources": {
-        "RootRole": {
+        "PorterRole": {
             "Type": "AWS::IAM::Role",
             "Properties": {
                 "AssumeRolePolicyDocument": {
@@ -19,7 +19,7 @@
                                     "sts:ExternalId": {
                                         "Ref": "ExternalIdParameter"
                                     }
-                                }
+                                }   
                             },
                             "Action": [
                                 "sts:AssumeRole"
@@ -56,7 +56,8 @@
                     "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy",
                     "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly",
                     "arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy"
-                ]
+                ],
+                "RoleName": "PorterRole"
             }
         }
     },