Skip to content

Commit

Permalink
Add request letsencrypt certificate (#508)
Browse files Browse the repository at this point in the history
* Add request-letsencrypt-certificate

* Add request-letsencrypt-certificate

* Add request-letsencrypt-certificate

* Add request-letsencrypt-certificate
  • Loading branch information
sanjaykesavan committed Sep 22, 2023
1 parent fc7f142 commit 7eacb62
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
27 changes: 27 additions & 0 deletions bin/test-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,33 @@ else
exit 1
fi

echo "Testing command ´sfcc-ci sandbox:alias:add´ with request for letsencrypt:"
ALIAS_RESULT=`node ./cli.js sandbox:alias:add --sandbox $TEST_NEW_SANDBOX_ID -h my.newalias.com --unique --request-letsencrypt-certificate --json`
if [ $? -eq 0 ]; then
echo -e "\t> OK"
else
echo -e "\t> FAILED"
exit 1
fi

echo "Testing command ´sfcc-ci sandbox:alias:add´ with request for letsencrypt and unique as false ( should fail ) :"
ALIAS_RESULT=`node ./cli.js sandbox:alias:add --sandbox $TEST_NEW_SANDBOX_ID -h my.newalias.com --request-letsencrypt-certificate --json`
if [ $? -eq 1 ]; then
echo -e "\t> OK"
else
echo -e "\t> FAILED"
exit 1
fi

TEST_NEW_ALIAS_ID=`echo $ALIAS_RESULT | jq '.id' -r`
echo "Testing command ´sfcc-ci sandbox:alias:delete´ (invalid alias):"
node ./cli.js sandbox:alias:delete --sandbox $TEST_NEW_SANDBOX_ID -a $TEST_NEW_ALIAS_ID --noprompt
if [ $? -eq 0 ]; then
echo -e "\t> OK"
else
echo -e "\t> FAILED"
exit 1
fi
###############################################################################
###### Testing ´sfcc-ci instance:clear´
###############################################################################
Expand Down
18 changes: 16 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,8 @@ program
.option('-h, --host <host>','hostname alias to register')
.option('-j, --json', 'Optional, formats the output in json')
.option('-u, --unique', 'Optional, define alias as unique, false by default')
.option('-l, --request-letsencrypt-certificate', 'Optional, ' +
'Request Letsencrypt certificate, false by default')
.description('Registers a hostname alias for a sandbox.')
.action(function(options) {
var sandbox = options.sandbox;
Expand All @@ -1071,7 +1073,13 @@ program
}
var asJson = ( options.json ? options.json : false );
var unique = ( options.unique ? options.unique : false );
require('./lib/sandbox').cli.alias.create(spec, aliasName, unique, asJson);
var requestLetsncrypt =
( options.requestLetsencryptCertificate ? options.requestLetsencryptCertificate : false );
if (requestLetsncrypt === true && unique === false) {
this.missingArgument('unique');
return;
}
require('./lib/sandbox').cli.alias.create(spec, aliasName, unique, asJson, requestLetsncrypt);
}).on('--help', function() {
console.log('');
console.log(' Details:');
Expand All @@ -1084,7 +1092,12 @@ program
console.log(' Use the --unique flag allows you to configure the alias to be unique across all aliases');
console.log(' registered. This requires that you have to proof ownership of the host on a DNS level.');
console.log(' The domain verification record value is generated and returned. By default the alias is not');
console.log(' unique.');
console.log(' unique. If requesting for a Letsencrypt certificate, this flag needs to be true');
console.log('');
console.log(' Use the --request-letsencrypt-certificate flag allows you to request a valid certificate ');
console.log(' to be generated on the fly through Lets Encrypt. This action consumes certificate requests ');
console.log(' from the domain quota imposed by Lets Encrypt, please read the Alias documentation ');
console.log(' carefully. You have to specify --unique flag as true when you enable this flag.');
console.log('');
console.log(' Use --json to only print the created alias incl. either the registration link or the');
console.log(' the domain verification record.');
Expand All @@ -1093,6 +1106,7 @@ program
console.log();
console.log(' $ sfcc-ci sandbox:alias:add -s my-sandbox-id -h sbx1.merchant.com');
console.log(' $ sfcc-ci sandbox:alias:add -s my-sandbox-id -h sbx1.merchant.com -j');
console.log(' $ sfcc-ci sandbox:alias:add -s my-sandbox-id -h sbx1.merchant.com -u true -l true -j');
console.log();
});

Expand Down
10 changes: 6 additions & 4 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,12 +782,13 @@ function doCookieRegistration(realm, link, host) {
* @param sbxID ID of the sandbox to create alias for
* @param alias name of the alias to create
* @param unique true, if the alias is unique across aliases, false by default
* @param requestLetsncrypt true, if need to request for Letsencrypt certificate, false by default
* @param {Function} callback the callback to execute, the error and the created alias are available as arguments to the callback function
*/
function registerForSandbox(sbxID, alias, unique, callback) {
function registerForSandbox(sbxID, alias, unique, requestLetsncrypt, callback) {
// the payload
var options = ocapi.getOptions('POST', API_SANDBOXES + '/' + sbxID + '/aliases');
options['body'] = {name: alias, unique: !!unique};
options['body'] = {name: alias, unique: !!unique, requestLetsEncryptCertificate : !!requestLetsncrypt};

ocapi.retryableCall('POST', options, function (err, res) {
if (err) {
Expand Down Expand Up @@ -1669,10 +1670,11 @@ module.exports.cli = {
* @param {String} alias the alias name to register for the sandbox
* @param {Boolean} unique optional flag to configure the alias to be unique, false by default
* @param {Boolean} asJson optional flag to force output in json, false by default
* @param {Boolean} requestLetsncrypt optional flag to request a new certificate, false by default
*/
create : function (spec, alias, unique, asJson) {
create : function (spec, alias, unique, asJson, requestLetsncrypt) {
runForSandbox(spec, asJson, function (sandbox) {
registerForSandbox(sandbox.id, alias, unique, function (err, result) {
registerForSandbox(sandbox.id, alias, unique, requestLetsncrypt, function (err, result) {
if (err) {
if (asJson) {
console.json({error: err.message});
Expand Down
8 changes: 8 additions & 0 deletions test/unit/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ describe('Alias Tests for lib/sandbox.js', function() {
responseHandler("Alias not found.", {statusCode: 404, body: {}});
return
}
if (url.includes('nonUniqueAlias')) {
responseHandler("Can not request certificate for a non-unique alias.", {statusCode: 409, body: {}});
return
}
if (url.endsWith('/aliases')) {
if (method === 'GET') {
responseHandler("", {statusCode: 200, body: aliasList});
Expand Down Expand Up @@ -168,6 +172,10 @@ describe('Alias Tests for lib/sandbox.js', function() {
sandbox.cli.alias.create({id: "unknownSandboxId"},"alias", false, false);
sinon.assert.calledWith(consError, "Cannot find sandbox with given ID.");
});
it('Create SBX alias with cert request', () => {
sandbox.cli.alias.create({id: "s1"},"alias", false, true, true);
sinon.assert.calledWith(consJson, existingAlias.data);
});
})

describe('List Aliases', function() {
Expand Down

0 comments on commit 7eacb62

Please sign in to comment.