Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
15
82 changes: 81 additions & 1 deletion lib/provider/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const yc = require('yandex-cloud');
const yaml = require('yaml');
const fs = require('fs');
const path = require('path');
const https = require('https');
const jose = require('node-jose');

const AWS = require('aws-sdk');

Expand Down Expand Up @@ -66,7 +68,85 @@ class YandexCloudProvider {
return;
}
const config = readCliConfig();
const session = new yc.Session({oauthToken: config.token});

let session;

if(config.token !== "undefined"){
session = new yc.Session({oauthToken: config.token});
}

if(config['service-account-key'] !== "undefined"){

const serviceAccountId = config['service-account-key']['service_account_id'];
const keyId = config['service-account-key']['id'];
const key = config['service-account-key']['private_key'];

const now = Math.floor(new Date().getTime() / 1000);

const payload = {
aud: "https://iam.api.cloud.yandex.net/iam/v1/tokens",
iss: serviceAccountId,
iat: now,
exp: now + 3600
};

let JWKBaseKeyObject = await jose.JWK.asKey(key, 'pem', { kid: keyId, alg: 'PS256' });
let jwt = await jose.JWS.createSign({ format: 'compact' }, JWKBaseKeyObject).update(JSON.stringify(payload)).final();

const data = JSON.stringify({
jwt: jwt
});

const options = {
hostname: 'iam.api.cloud.yandex.net',
port: 443,
path: '/iam/v1/tokens',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}

let iamToken = await (async () => {

return new Promise(function(resolve, reject) {

const req = https.request(options, (res) => {

let body = '';

res.setEncoding("utf8");

res.on('data', chunk => {
body += chunk;
});

res.on('end', () => {

try {
body = JSON.parse(body);
} catch(e) {
reject(e);
}

resolve(body);

});

});

req.write(data);
req.end();

});

})();

session = new yc.Session({iamToken: iamToken.iamToken});

}

if (config.endpoint) {
await session.setEndpoint(config.endpoint);
}
Expand Down
1 change: 1 addition & 0 deletions templates/nodejs/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
15
2 changes: 1 addition & 1 deletion templates/nodejs/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
service: yandex-cloud-nodejs
name: yandex-cloud-template
frameworkVersion: ">=1.1.0 <2.0.0"
frameworkVersion: "2"

provider:
name: yandex-cloud
Expand Down