【问题标题】:AWS AppSync using wrong Iam roleAWS AppSync 使用错误的 Iam 角色
【发布时间】:2018-10-13 03:14:36
【问题描述】:

我正在使用包括 dynamodb 和 s3 在内的服务与 ios 一起用于自定义登录用户池,该用户池由联合身份使用,以便为经过身份验证和未经身份验证的用户提供角色。这很完美。

然后,当我使用 AWS AppSync 时,我只是在执行一个 Mutation,我收到一条错误消息 Unable to take role arn:aws:iam::xxxxxxxxxx:role/xxxxxxxxx 。 AppSync 尝试使用的这个 arn:aws:iam:xxx/xxx 角色不是我想要使用的角色,并且 iam 角色与 DynamoDb 和 s3 出于某种原因使用的角色不同,即使我是不更改代码中使用的任何 cognitoid。请参阅下面的一些示例代码。

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// CognitoIdentityUserPoolId is the id of the federated identity from console. works fine with all services except for AppSync by wrong Iam role being chosen
  let serviceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)

  let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: CognitoIdentityUserPoolAppClientId, clientSecret: CognitoIdentityUserPoolAppClientSecret, poolId: CognitoIdentityUserPoolId)

  AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: AWSCognitoUserPoolsSignInProviderKey)

  pool = AWSCognitoIdentityUserPool(forKey: AWSCognitoUserPoolsSignInProviderKey)

  let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: CognitoIdentityUserPoolId, identityProviderManager:pool)

  let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)

  AWSServiceManager.default().defaultServiceConfiguration = configuration

  pool?.delegate = self

  return true 
}
// In SampleViewController
  // after user signs in
  let appSyncConfig = try AWSAppSyncClientConfiguration(url: AppSyncEndpointURL, serviceRegion: CognitoIdentityUserPoolRegion, userPoolsAuthProvider: self)

  // Initialize the AWS AppSync client
  self.appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig)

  // Set id as the cache key for objects
  self.appSyncClient?.apolloClient?.cacheKeyForObject = { $0["id"] }

// later on in the file....
extension SampleViewController: AWSCognitoUserPoolsAuthProvider{
  func getLatestAuthToken() -> String {
    return (myUserSess?.idToken?.tokenString)!
  }
}

AWS AppSync 尝试使用错误的 Iam 名称的任何帮助/原因?

【问题讨论】:

    标签: ios amazon-dynamodb amazon-cognito amazon-iam aws-appsync


    【解决方案1】:

    你从哪里得到这条消息?使用查询页面时在控制台中还是在客户端上?如果它在控制台中,则问题可能与您的数据源的 IAM 角色有关,重新创建它或确保您具有以下信任策略可能是有意义的:

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Principal": {
            "Service": "appsync.amazonaws.com"
          },
          "Action": "sts:AssumeRole"
        }
      ]
    }
    

    如果它在客户端,则错误可能是在您初始化 Cognito Identity 提供程序或 AppSync 构造函数时。

    【讨论】:

    • 我在客户端收到消息。错误的来源是 AWSAppSync.GraphQLResult.Source.server。数据源是一个 dynamodb 表,我用来访问 DynamoDb 的 Iam 角色具有完全访问权限,并且在不通过 AppSync 从客户端读取和写入时工作。问题是由于某种原因试图为 AppSync 假定错误的 Iam 角色,您知道这是为什么吗?我刚刚在 AppSync 控制台上添加了 Mutation 的数据源,使其成为我在 DynamoDB 控制台上手动创建的表。
    • @Rekd 那么问题在于授予客户端的 Cognito 角色。首先查看有关该角色的 IAM 策略,以确保它可以访问您的 AppSync 端点(可能暂时打开它,然后在可行的情况下限制它)。如果这不是问题,那么就是您调用 Cognito 的顺序/语法。
    • 我将权限添加到了我在 sts:assumerole 和 appsync:graphql 的帖子中看到的身份验证角色,但我添加的角色由于某种原因并未尝试被假定。我不认为客户端上的 cognitoid 和 userpoolid 不好,因为客户端可以假设 auth 角色来访问策略中指定的资源,例如 DynamoDb 直接读写。有什么我可以在 AppSync 控制台上做的事情,或者创建其他 Iam 角色来处理这个问题,就像我目前在 AppSync 控制台上一样,以允许我的应用程序用户池似乎不承担该用户池的身份验证角色。
    【解决方案2】:

    确保与 Appsync 一起使用的角色能够调用 Lambda 函数,并且与解析程序 Lambda 关联的角色应具有足够的权限来访问您尝试查询的数据库。

    另外,如果有任何政策不适用于 AppSync,它会抛出这样的错误。

    【讨论】:

      猜你喜欢
      • 2018-08-19
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      • 2021-08-13
      • 2019-05-10
      • 2023-04-03
      • 2018-11-10
      相关资源
      最近更新 更多