【问题标题】:AWS DynamoDB access from Android, access denied to table从 Android 访问 AWS DynamoDB,拒绝访问表
【发布时间】:2018-04-24 13:02:09
【问题描述】:

我正在尝试在 NoSQL DynamoDB 上执行 CRUD 操作,我最初没有将 CognitoPool 与我在 AWS Mobile Hub 上的项目集成,但我没有这样做,并替换了我的“原始”目录中的我的 json 文件项目。该数据库是完全公开的读写,但由于某种原因我不断收到此错误:

com.amazonaws.AmazonServiceException: User: arn:aws:sts::1234567890:assumed-role/shoppinglist_unauth_MOBILEHUB_1234567890/CognitoIdentityCredentials is not authorized to perform: dynamodb:DescribeTable on resource: arn:aws:dynamodb:us-east-1:1234567890:table/ShoppingLists (Service: AmazonDynamoDB; Status Code: 400; Error Code: AccessDeniedException; Request ID: BQ0HAP7PUGO6AUC04LOHUND1V3VV4KQNSO5AEMVJF66Q9ASUAAJG)

出于安全原因,我已将所有识别号码更改为 1234567890。

这是我的 .json 文件:

{
  "UserAgent": "MobileHub/1.0",
  "Version": "1.0",
  "CredentialsProvider": {
    "CognitoIdentity": {
      "Default": {
        "PoolId": "us-east-1******************,
        "Region": "us-east-1"
      }
    }
  },
  "IdentityManager": {
    "Default": {}
  },
  "CognitoUserPool": {
    "Default": {
      "PoolId": "us-east-1_*******",
      "AppClientId": "5lg571jsd60ruvair8jiqpefbs",
      "AppClientSecret": "bqn8edlp19gfgogfhf4j9qg1mq8u8ftpb328f652n0451gl2dnt",
      "Region": "us-east-1"
    }
  },
  "DynamoDBObjectMapper": {
    "Default": {
      "Region": "us-east-1"
    }
  },
  "PinpointAnalytics": {
    "Default": {
      "AppId": "27e0f3ee2e63419c9dc8f18f23a294fe",
      "Region": "us-east-1"
    }
  },
  "PinpointTargeting": {
    "Default": {
      "Region": "us-east-1"
    }
  }
}

这是我的主要活动类中的 onCreate() 方法

AWSMobileClient.getInstance().initialize(this, awsStartupResult ->
        Log.d("YourMainActivity", "AWSMobileClient is instantiated and you are connected to AWS!"))
        .execute();

// Instantiate a AmazonDynamoDBMapperClient
AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(AWSMobileClient.getInstance().getCredentialsProvider());
this.dynamoDBMapper = DynamoDBMapper.builder()
        .dynamoDBClient(dynamoDBClient)
        .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
        .build();

Runnable runnable = () -> {
    dbClient = new AmazonDynamoDBClient(AWSMobileClient.getInstance().getCredentialsProvider());
    // Create a table reference
    dbTable = Table.loadTable(dbClient, "ShoppingLists");
    Document memo = new Document();
    memo.put("Apple", "apple");
    dbTable.putItem(memo);
};
Thread myThread = new Thread(runnable);
myThread.start();

我的 build.gradle 应该包含正确的依赖关系,这些都在这里,当然它可能有点混乱:

    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation ('com.amazonaws:aws-android-sdk-mobile-client:2.6.+@aar') { transitive = true }
    implementation 'com.amazonaws:aws-android-sdk-core:2.6.18'
    implementation 'com.amazonaws:aws-android-sdk-s3:2.6.18'
    implementation 'com.amazonaws:aws-android-sdk-ddb:2.6.18'
    implementation 'com.amazonaws:aws-android-sdk-ddb-mapper:2.6.18'
    compile 'com.amazonaws:aws-android-sdk-core:2.6.18'
    compile 'com.amazonaws:aws-android-sdk-ddb:2.6.18'
    compile 'com.amazonaws:aws-android-sdk-ddb-document:2.4.4'
    // Mobile Client for initializing the SDK
    implementation ('com.amazonaws:aws-android-sdk-mobile-client:2.6.+@aar') { transitive = true }

    // Cognito UserPools for SignIn
    implementation 'com.android.support:support-v4:27.1.1'
    implementation ('com.amazonaws:aws-android-sdk-auth-userpools:2.6.+@aar') { transitive = true }

    // Sign in UI Library
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation ('com.amazonaws:aws-android-sdk-auth-ui:2.6.+@aar') { transitive = true }

据我所知,这应该可以正常工作,我在这里得到了另一个乐于助人的人的帮助,以达到它可以连接到 AWS 的地步,它曾经做过,但我似乎无法访问表。

我想我有两个问题,是否有可能在没有 Cognito 池的情况下做到这一点并且让它完全不安全?如果没有,我如何让它与 Cognito Pool 一起使用?认知池是否应该有与之关联的用户名?

【问题讨论】:

    标签: android amazon-web-services


    【解决方案1】:

    让我来回答你的问题

    1) 你可以在没有 Cognito 身份池的情况下做到这一点吗?答案是肯定的。这并不自动意味着它不安全 - 您可以使用您为 IAM 用户设置的 AWS 凭证。然而,最佳实践是使用 Cognito 身份池,它会为您提供在短时间内有效的临时 AWS 凭证。

    2) 当您创建 Cognito 身份池时,会自动创建两个角色(名为 auth 和 unauth)。 “auth”角色适用于您有登录用户的情况(例如,使用 Facebook 等社交登录名登录并通过身份池联合到 AWS),而“unauth”角色适用于尚未登录的用户尚未登录。您可以使用 IAM 策略微调这些角色的访问权限,以最适合您的应用程序的需求。

    请参阅此页面 (https://docs.aws.amazon.com/aws-mobile/latest/developerguide/how-to-nosql-integrate-an-existing-table.html),了解如何使用此功能的具体说明。

    【讨论】:

    • 这个加上另一个人,你们让我很开心。效果很好,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2021-07-04
    • 2017-12-24
    • 2021-11-06
    • 2020-05-19
    • 1970-01-01
    相关资源
    最近更新 更多