3

我正在尝试运行此 AWS CLI 命令:

aws ecr get-login --no-include-email --region <my-region>

但是使用带有方法的 JavaScript SDKnew AWS.ECR(..).getAuthorizationToken但我收到了这个错误:

Error response from daemon: login attempt to https://xxxxx.dkr.ecr.<my-region>.amazonaws.com/v2/ failed with status: 400 Bad Request

据我了解,我需要使用一些--no-include-email与 CLI 命令中的参数相等的标志,但我找不到如何使用 JavaScript SDK 设置它。

这是我的代码:

const ecr = new AWS.ECR({
    apiVersion: '2015-09-21',
    region: 'my-region'
});

const { authorizationData } = await ecr.getAuthorizationToken().promise();

if(!authorizationData || !authorizationData[0] || !authorizationData[0].authorizationToken){
    throw new Error('AWS getAuthorizationToken failed');
}

const password = authorizationData[0].authorizationToken;
const proxyEndpoint = authorizationData[0].proxyEndpoint;

await childProcessP.spawn('docker', [
    'login',
    '-u', 'AWS',
    '-p', password,
    proxyEndpoint
]);

有谁知道该怎么做?

4

3 回答 3

1

the AuthorizationToken contains a base64 encoded <username>:<password>

all you need to do is decode and split to get the values you want.

于 2018-08-22T20:51:14.767 回答
0

通过使用(golang)docker ENGINE 库,我遇到了同样的问题。

首先,我从ecr.GetAuthorizationToken() Then i call docker.RegistryLogin()函数中获得凭据(像你一样)。

这是我的代码:

authToken, err  := ecrSession.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{})
    if err != nil {
        color.Red(fmt.Sprintf("Getting ECR auth token: %s", err.Error()))
        return err
    }

    auth := types.AuthConfig{
        Username: "AWS",
        Password: *authToken.AuthorizationData[0].AuthorizationToken,
        ServerAddress: *authToken.AuthorizationData[0].ProxyEndpoint,

    }

     authenticatedOK, err := cli.RegistryLogin(ctx, auth)

总是返回给我400 bad request

但是,通过从 shell 运行命令:

aws ecr get-login --no-include-email --region eu-west-1

然后复制粘贴返回的输出

docker login -u AWS -p ... http://account-id.dkr.ecr.eu-west-1.amazonaws.com它工作正常。

于 2018-05-08T15:18:27.280 回答
0

我遇到了同样的问题,我在文档中发现 authToken.AuthorizationData[0].AuthorizationToken 中的令牌返回是一个 base 64 编码的字符串。

所以如果你想用它来登录你的码头,你首先要解码它。

解码后,您将看到如下内容:AWS:eyJwYXlsb2FkIjoiZGhtVzc5Sj....

事实证明,令牌是用用户编码的。因此,您必须取出 AWS: 并使用剩余字符串。

于 2019-05-17T22:14:44.713 回答