BUGFIX: non-secret variables

This commit is contained in:
Jeroen De Meerleer 2021-05-06 13:30:12 +02:00
parent a3efea0e83
commit 1ea40525f9
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG Key ID: 28CCCB8F62BFADD6
3 changed files with 64 additions and 26 deletions

View File

@ -1,7 +1,7 @@
$(function() {
initDatePickers();
initCronType();
initSecretInputs();
initVarInputs();
initDelayPattern();
bsCustomFileInput.init()
});
@ -25,11 +25,21 @@ function initCronType()
})
}
function initSecretInputs()
function initVarInputs()
{
$('.addsecret-btn').on('click', function() {
$('.secret-group:first-child').clone().appendTo('.secrets').removeClass('hidden');
$('.secrets-description').removeClass('hidden');
$('.addvar-btn').on('click', function() {
let index = $('.var-group').length;
$('.var-group:first-child').clone().appendTo('.vars').removeClass('hidden');
$('.var-group:last-child').data({index: index});
$('.var-group:last-child .var-issecret').prop('name', 'var-issecret[' + index + ']');
$('.var-group:last-child .var-id').prop('name', 'var-id[' + index + ']');
$('.var-group:last-child .var-value').prop('name', 'var-value[' + index + ']');
$('.vars-description').removeClass('hidden');
})
$(document).on('click', '.var-issecret', function() {
let ischecked = $(this).prop('checked');
$(this).parents('.var-group').find('.var-value').prop('type', ischecked ? 'password' : 'text');
})
}

View File

@ -56,10 +56,17 @@ class Job
case 'ssh':
$data['host'] = $values['host'];
$data['user'] = $values['user'];
if(!empty($values['privkey-password'])) {
$newsecretkey = count($values['var-value']);
$values['var-id'][$newsecretkey] = 'privkey-password';
$values['var-issecret'][$newsecretkey] = true;
$values['var-value'][$newsecretkey] = $values['privkey-password'];
}
if(!empty($_FILES['privkey']['tmp_name'])) {
$newsecretkey = count($values['secretval']);
$values['secretid'][$newsecretkey] = 'ssh-privkey';
$values['secretval'][$newsecretkey] = base64_encode(file_get_contents($_FILES['privkey']['tmp_name']));
$newsecretkey = count($values['var-value']);
$values['var-id'][$newsecretkey] = 'ssh-privkey';
$values['var-issecret'][$newsecretkey] = true;
$values['var-value'][$newsecretkey] = base64_encode(file_get_contents($_FILES['privkey']['tmp_name']));
}
$data['command'] = $values['command'];
break;
@ -70,13 +77,27 @@ class Job
if(empty($parsedUrl['host'])) {
return ['success' => false, 'message' => 'Some data was invalid'];
}
if(!empty($values['basicauth-password'])) {
$newsecretkey = count($values['var-value']);
$values['var-id'][$newsecretkey] = 'basicauth-password';
$values['var-issecret'][$newsecretkey] = true;
$values['var-value'][$newsecretkey] = $values['basicauth-password'];
}
$data['host'] = $parsedUrl['host'];
break;
}
if(!empty($values['secretval'])) {
foreach($values['secretval'] as $key => $name) {
if(!empty($name)) $data['secrets'][$values['secretid'][$key]] = base64_encode(Secret::encrypt($values['secretval'][$key]));
if(!empty($values['var-value'])) {
foreach($values['var-value'] as $key => $name) {
if(!empty($name)) {
if(isset($values['var-issecret'][$key])) {
$data['vars'][$values['var-id'][$key]]['issecret'] = true;
$data['vars'][$values['var-id'][$key]]['value'] = base64_encode(Secret::encrypt($values['var-value'][$key]));
} else {
$data['vars'][$values['var-id'][$key]]['issecret'] = false;
$data['vars'][$values['var-id'][$key]]['value'] = $values['var-value'][$key];
}
}
}
}
@ -95,9 +116,11 @@ class Job
$jobRslt = $jobStmt->execute([':id' => $id])->fetchAssociative();
$jobRslt['data'] = json_decode($jobRslt['data'], true);
if(!empty($jobRslt['data']['secrets'])) {
foreach ($jobRslt['data']['secrets'] as $key => &$value) {
$value = ($withSecrets) ? Secret::decrypt(base64_decode($value)) : '';
if(!empty($jobRslt['data']['vars'])) {
foreach ($jobRslt['data']['vars'] as $key => &$value) {
if ($value['issecret']) {
$value['value'] = ($withSecrets) ? Secret::decrypt(base64_decode($value['value'])) : '';
}
}
}

View File

@ -84,8 +84,7 @@
<div class="mb-3">
<label for="privkey-password">Password for private key</label>
<input type="hidden" name="secretid[]" value="privkey-password">
<input type="password" name="secretval[]" class="form-control" placeholder="correct horse battery staple">
<input type="password" name="privkey-password" class="form-control" placeholder="correct horse battery staple">
<small id="privkey-password-help" class="form-text text-muted">If private key is empty this field is being used as ssh-password</small>
<small id="privkey-password-help-2" class="form-text text-muted">This field is being saved as a secret</small>
</div>
@ -107,27 +106,33 @@
</div>
<div class="mb-3">
<label for="basicauth-password">Password for Basic-Auth</label>
<input type="hidden" name="secretid[]" value="basicauth-password">
<input type="password" name="secretval[]" class="form-control" placeholder="correct horse battery staple">
<input type="password" name="basicauth-password" class="form-control" placeholder="correct horse battery staple">
<small id="basicauth-password-help" class="form-text text-muted">This field is being saved as a secret</small>
</div>
</div>
<h3>Secrets</h3>
<div class="secrets mb-3">
<div class="input-group secret-group hidden">
<input type="text" name="secretid[]" class="form-control" placeholder="name">
<input type="password" name="secretval[]" class="form-control" placeholder="value">
<h3>Variables</h3>
<div class="vars mb-3">
<div class="input-group var-group hidden">
<div class="input-group-prepend">
<div class="input-group-text border-right-0">
<input type="checkbox" name="var-issecret[0]" class="var-issecret" placeholder="value" value="true">
</div>
<span class="input-group-text border-left-0">Secret</span>
</div>
<input type="text" name="var-id[0]" class="form-control var-id" placeholder="name">
<input type="text" name="var-value[0]" class="form-control var-value" placeholder="value">
</div>
</div>
<div class="secrets-description mb-3 hidden">
<div class="vars-description mb-3 hidden">
<p>
You can add secrets by using {secret-name} in job details
You can add variables by using {variable-name} in job details
</p>
</div>
<div class="mb-3">
<a href="#" class="btn btn-outline-primary addsecret-btn">Add secret</a>
<a href="#" class="btn btn-outline-primary addvar-btn">Add variable</a>
</div>
<input type="hidden" name="type" class="crontype" value=""><button type="submit" class="btn btn-outline-primary">Submit</button>
</form>