AWS无法验证提供的访问凭据

时间:2014-12-29 07:52:13

标签: java amazon-web-services amazon-ec2 amazon-s3 aws-sdk

我一直在尝试使用AWS SDK创建安全组,但不知何故它无法对其进行身份验证。对于特定的访问密钥和密钥,我提供了管理权限,然后它也无法验证。另一方面,我在AWS S3示例中尝试了相同的凭据,它成功执行。

创建安全组时出现以下错误:

com.amazonaws.AmazonServiceException: AWS was not able to validate the provided access credentials (Service: AmazonEC2; Status Code: 401; Error Code: AuthFailure; Request ID: 1584a035-9a88-4dc7-b5e2-a8b7bde6f43c)
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1077)
    at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:725)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:460)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:295)
    at com.amazonaws.services.ec2.AmazonEC2Client.invoke(AmazonEC2Client.java:9393)
    at com.amazonaws.services.ec2.AmazonEC2Client.createSecurityGroup(AmazonEC2Client.java:1146)
    at com.sunil.demo.ec2.SetupEC2.createSecurityGroup(SetupEC2.java:84)
    at com.sunil.demo.ec2.SetupEC2.main(SetupEC2.java:25)

这是Java代码:

public class SetupEC2 {
    AWSCredentials credentials = null;
    AmazonEC2Client amazonEC2Client ;

    public static void main(String[] args) {
        SetupEC2 setupEC2Instance = new SetupEC2();
        setupEC2Instance.init();
        setupEC2Instance.createSecurityGroup();
    }

    public void init(){
        // Intialize AWS Credentials
        try {
            credentials = new BasicAWSCredentials("XXXXXXXX", "XXXXXXXXX");
        } catch (Exception e) {
            throw new AmazonClientException(
                    "Cannot load the credentials from the credential profiles file. " +
                            "Please make sure that your credentials file is at the     correct " +
                            "location (/home/sunil/.aws/credentials), and is in valid format.",
                            e);
        }

        // Initialize EC2 instance
        try {
            amazonEC2Client = new AmazonEC2Client(credentials);
            amazonEC2Client.setEndpoint("ec2.ap-southeast-1.amazonaws.com");
            amazonEC2Client.setRegion(Region.getRegion(Regions.AP_SOUTHEAST_1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean createSecurityGroup(){
        boolean securityGroupCreated = false;
        String groupName = "sgec2securitygroup";
        String sshIpRange = "0.0.0.0/0";
        String sshprotocol = "tcp";
        int sshFromPort = 22;
        int sshToPort =22;

        String httpIpRange = "0.0.0.0/0";
        String httpProtocol = "tcp";
        int httpFromPort = 80;
        int httpToPort = 80;

        String httpsIpRange = "0.0.0.0/0";
        String httpsProtocol = "tcp";
        int httpsFromPort = 443;
        int httpsToProtocol = 443;

        try {
            CreateSecurityGroupRequest createSecurityGroupRequest =  new CreateSecurityGroupRequest();
            createSecurityGroupRequest.withGroupName(groupName).withDescription("Created from AWS SDK Security Group");
            createSecurityGroupRequest.setRequestCredentials(credentials);

            CreateSecurityGroupResult csgr = amazonEC2Client.createSecurityGroup(createSecurityGroupRequest);

            String groupid = csgr.getGroupId();
            System.out.println("Security Group Id : " + groupid);

            System.out.println("Create Security Group Permission");
            Collection<IpPermission> ips = new ArrayList<IpPermission>();
            // Permission for SSH only to your ip
            IpPermission ipssh = new IpPermission();
        ipssh.withIpRanges(sshIpRange).withIpProtocol(sshprotocol).withFromPort(sshFromPort).withToPort(sshToPort);
            ips.add(ipssh);

            // Permission for HTTP, any one can access
            IpPermission iphttp = new IpPermission();
        iphttp.withIpRanges(httpIpRange).withIpProtocol(httpProtocol).withFromPort(httpFromPort).withToPort(httpToPort);
            ips.add(iphttp);

            //Permission for HTTPS, any one can accesss
            IpPermission iphttps = new IpPermission();
            iphttps.withIpRanges(httpsIpRange).withIpProtocol(httpsProtocol).withFromPort(httpsFromPort).withToPort(httpsToProtocol);
            ips.add(iphttps);

            System.out.println("Attach Owner to security group");
            // Register this security group with owner
            AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest();
            authorizeSecurityGroupIngressRequest.withGroupName(groupName).withIpPermissions(ips);
            amazonEC2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);
        securityGroupCreated = true;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            securityGroupCreated = false;
        }
        System.out.println("securityGroupCreated: " + securityGroupCreated);
        return securityGroupCreated;
    }
}

3 个答案:

答案 0 :(得分:18)

尝试更新您的系统时间。

当AWS-datetime与您的日期时间之间的差异太大时,将不接受凭据。

对于Debian / Ubuntu用户:

如果您从未设置时区,则可以使用

执行此操作
sudo dpkg-reconfigure tzdata

停止ntp-Service,因为太大的时间差异,无法通过运行服务来改变。

sudo /etc/init.d/ntp stop

同步您的时间和日期(-q设置时间并退出/仅运行一次)( - g允许第一次调整为大)(-x转换为600秒/ Adjuste也是时间大的差异)( - n不要分叉/处理不会去背景)

sudo ntpd -q -g -x -n

重启服务

sudo /etc/init.d/ntp start

检查实际的system-datetime

sudo date

将system-datetime设置为hardware-datetime

sudo hwclock --systohc

显示您的硬件日期时间

sudo hwclock

答案 1 :(得分:0)

如果您将凭据放在〜/ .aws / credentials中,则无需为AmazonEC2Client调用提供参数。如果您这样做,那么在EC2实例上,相同的代码将与假定的STS角色一起使用。

有关详细信息,请参阅:http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/credentials.html

答案 2 :(得分:0)

“调用[填充空白]操作时发生客户端错误(AuthFailure):AWS无法验证提供的访问凭据”

  1. 如果您对AWS凭证的有效性有信心,即访问密钥和密钥以及相应的个人资料名称,那么您的偏离日期和时间是一个非常好的罪魁祸首。

  2. 就我而言,我很自信,但我错了 - 我使用了错误的钥匙。双重检查不会有害。

  3. 假设您创建了一个名为“guignol”的IAM用户。在〜/ .aws / config 中配置“guignol”,如下所示:

    [profile guignol] region = us-east-1 aws-access-key_id = AKXXXYYY... aws-secret-key-access = ...

  4. 如果尚未安装aws cli(命令级接口),请安装它。作为测试,运行 aws ec2 describe-instances --profile guignol 如果你收到aws无法验证凭据的错误消息,请运行 aws configure --profile guignol ,输入您的凭据并再次运行测试命令。