5

Tôi muốn gọi số AWS API Gateway Endpoint được bảo vệ bằng AWS_IAM bằng cách sử dụng generated JavaScript API SDK.Làm cách nào để gọi Điểm cuối Cổng API AWS với Id ẩn danh (+ cấu hình)?

Tôi có một số Cognito UserPoolCognito Identity Pool. Cả hai được đồng bộ hóa chính xác qua ClientId.

tôi sử dụng mã này để Sign in và nhận được Cognito Identity

AWS.config.region = 'us-east-1'; // Region 
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 
    IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXXXXXX' // your identity pool id here 
}); 

AWSCognito.config.region = 'us-east-1'; 
AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({ 
    IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXXXXXX' // your identity pool id here 
}); 

var poolData = { 
    UserPoolId: 'us-east-1_XXXXXXXX', 
    ClientId: 'XXXXXXXXXXXXXXXXXXXXXXXX' 
}; 
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); 


var authenticationData = { 
    Username: 'user', 
    Password: '12345678', 
}; 
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData); 
var userData = { 
    Username: 'user', 
    Pool: userPool 
}; 
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); 
cognitoUser.authenticateUser(authenticationDetails, { 
    onSuccess: function (result) { 
    console.log('access token + ' + result.getAccessToken().getJwtToken()); 

    AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 
    IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXX', 
    IdentityId: AWS.config.credentials.identityId, 
    Logins: { 
     'cognito-idp.us-east-1.amazonaws.com/us-east-1_XXXXXX': result.idToken.jwtToken 
    } 
    }); 

    AWS.config.credentials.get(function (err) { 
    // now I'm using authenticated credentials 
    if(err) 
    { 
     console.log('error in autheticatig AWS'+err); 
    } 
    else 
    { 
     console.log(AWS.config.credentials.identityId); 

    } 
    }); 
    }, 

    onFailure: function (err) { 
    alert(err); 
    } 

}); 

Tất cả điều này thành công và tôi có một authorized Cognito Identity bây giờ.

Bây giờ tôi cố gắng gọi số API Gateway Endpoint để thực hiện Lambda Function nó trỏ đến.

var apigClient = apigClientFactory.newClient({ 
    accessKey: AWS.config.credentials.accessKeyId, //'ACCESS_KEY', 
    secretKey: AWS.config.credentials.secretAccessKey, //'SECRET_KEY', 
    sessionToken: AWS.config.credentials.sessionToken, // 'SESSION_TOKEN', //OPTIONAL: If you are using temporary credentials you must include the session token 
    region: 'us-east-1' // OPTIONAL: The region where the API is deployed, by default this parameter is set to us-east-1 
    }); 

    var params = { 
    // This is where any modeled request parameters should be added. 
    // The key is the parameter name, as it is defined in the API in API Gateway. 
    }; 

    var body = { 
    // This is where you define the body of the request, 
    query: '{person {firstName lastName}}' 
    }; 

    var additionalParams = { 
    // If there are any unmodeled query parameters or headers that must be 
    // sent with the request, add them here. 
    headers: {}, 
    queryParams: {} 
    }; 

    apigClient.graphqlPost(params, body, additionalParams) 
    .then(function (result) { 
     // Add success callback code here. 
     console.log(result); 
    }).catch(function (result) { 
    // Add error callback code here. 
    console.log(result); 
    }); 

Nhưng tiếc là điều này không thành công. Yêu cầu OPTIONS thành công với 200 nhưng POST sau đó không thành công với 403.

Tôi khá chắc chắn rằng không có vấn đề nào về vấn đề này tại CORS.

Tôi khá chắc chắn rằng sự cố phải làm với IAM RolesAWS Resource Configurations.

Câu hỏi của tôi về cơ bản, bạn có thể vui lòng cung cấp cho tôi tất cả các yêu cầu AWS Resource ConfigurationsIAM Roles cần thiết để làm việc này không?

Tài nguyên tôi đã được

  • API Gateway - với Endpoints API triển khai
  • Lambda Chức năng - gọi bằng các Endpoint
  • Cognito tài có bể bơi - với App đồng bộ hóa với Identity Pool
  • Cognito nhận dạng Pool - với vai trò ủy quyền và trái phép được ánh xạ tới nó.
  • Vai trò IAM - cho chức năng Lambda và Vai trò được ủy quyền và trái phép của Hồ bơi nhận dạng bí danh.

Nhưng tôi không biết cách các Tài nguyên này cần được định cấu hình đúng cách để làm việc này.

Cảm ơn bạn

Trả lời

3

Quyền truy cập nào có vai trò của nhận dạng bí danh? Đảm bảo có quyền truy cập để thực hiện execute-api:Invoke trên API của bạn.

{ 
    "Version": "2012-10-17", 
    "Statement": [ 
    { 
     "Effect": "Allow", 
     "Action": [ 
     "execute-api:Invoke"   
     ], 
     "Resource": [ 
     "arn:aws:execute-api:us-east-1:<account>:<rest-api>/*/POST/graphql" 
     ] 
    } 
    ] 
} 

Bạn có thể lấy ARN tài nguyên chính xác từ trang cài đặt phương pháp trong bảng điều khiển web.

+0

Awesome, cảm ơn bạn. Đó là phần còn thiếu của câu đố. – Christine

1

Ngay cả sau khi làm theo mọi thứ tôi nhận được lỗi tương tự. Và lý do là tôi đã bỏ lỡ "sessionToken" trong khi khởi tạo apigClient.

var apigClient = apigClientFactory.newClient({ 
accessKey: AWS.config.credentials.accessKeyId, //'ACCESS_KEY', 
secretKey: AWS.config.credentials.secretAccessKey, //'SECRET_KEY', 
sessionToken: AWS.config.credentials.sessionToken, // 'SESSION_TOKEN', //OPTIONAL: If you are using temporary credentials you must include the session token 
region: 'us-east-1' // OPTIONAL: The region where the API is deployed, by default this parameter is set to us-east-1 }); 

// TỰ CHỌN THÊM: Nếu bạn đang sử dụng chứng chỉ tạm thời bạn phải bao gồm các phiên thẻ - không phải là thực sự không bắt buộc

+0

Với Cognito, bạn đang sử dụng thông tin đăng nhập tạm thời. Nhưng việc sử dụng Cognito không phải là tùy chọn, bạn chỉ có thể sử dụng thông tin đăng nhập chuẩn, nhưng không nên sử dụng. Dù sao, bạn * có thể *, vì vậy đó là lý do tại sao nhận xét đó nói rằng đó là tùy chọn. –

Các vấn đề liên quan