Skip to content

Commit

Permalink
Merge pull request #1846 from tothakos-code/develop
Browse files Browse the repository at this point in the history
Add secret survey var type, for passwords
  • Loading branch information
fiftin committed May 20, 2024
2 parents 599cd15 + a3a4a1c commit 22ed357
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions api-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ definitions:
type: string
environment:
type: string
secret:
type: string
limit:
type: string
TaskOutput:
Expand Down
1 change: 1 addition & 0 deletions db/Task.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Task struct {
Playbook string `db:"playbook" json:"playbook"`
Environment string `db:"environment" json:"environment"`
Limit string `db:"hosts_limit" json:"limit"`
Secret string `db:"-" json:"secret"`

UserID *int `db:"user_id" json:"user_id"`

Expand Down
12 changes: 12 additions & 0 deletions services/tasks/LocalJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strconv"
"maps"

"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/db_lib"
Expand All @@ -19,6 +20,7 @@ type LocalJob struct {
Inventory db.Inventory
Repository db.Repository
Environment db.Environment
Secret string
Logger task_logger.Logger

App db_lib.LocalApp
Expand Down Expand Up @@ -90,13 +92,23 @@ func (t *LocalJob) getEnvironmentExtraVars(username string, incomingVersion *str

func (t *LocalJob) getEnvironmentExtraVarsJSON(username string, incomingVersion *string) (str string, err error) {
extraVars := make(map[string]interface{})
extraSecretVars := make(map[string]interface{})

if t.Environment.JSON != "" {
err = json.Unmarshal([]byte(t.Environment.JSON), &extraVars)
if err != nil {
return
}
}
if t.Secret != "" {
err = json.Unmarshal([]byte(t.Secret), &extraSecretVars)
if err != nil {
return
}
}
t.Secret = "{}"

maps.Copy(extraVars, extraSecretVars)

taskDetails := make(map[string]interface{})

Expand Down
3 changes: 3 additions & 0 deletions services/tasks/TaskPool.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ func (p *TaskPool) AddTask(taskObj db.Task, userID *int, projectID int) (newTask
taskObj.Status = task_logger.TaskWaitingStatus
taskObj.UserID = userID
taskObj.ProjectID = projectID
extraSecretVars := taskObj.Secret
taskObj.Secret = "{}"

tpl, err := p.store.GetTemplate(projectID, taskObj.TemplateID)
if err != nil {
Expand Down Expand Up @@ -375,6 +377,7 @@ func (p *TaskPool) AddTask(taskObj db.Task, userID *int, projectID int) (newTask
Inventory: taskRunner.Inventory,
Repository: taskRunner.Repository,
Environment: taskRunner.Environment,
Secret: extraSecretVars,
Logger: app.SetLogger(&taskRunner),
App: app,
}
Expand Down
3 changes: 3 additions & 0 deletions web/src/components/SurveyVars.vue
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ export default {
}, {
id: 'int',
name: 'Integer',
}, {
id: 'secret',
name: 'Secret',
}, {
id: 'enum',
name: 'Enum',
Expand Down
19 changes: 18 additions & 1 deletion web/src/components/TaskForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,22 @@
/>

<div v-for="(v) in template.survey_vars || []" :key="v.name">

<v-text-field
v-if="v.type === 'secret'"
:label="v.title"
:hint="v.description"
v-model="editedSecretEnvironment[v.name]"
:required="v.required"
type="password"
:rules="[
val => !v.required || !!val || v.title + $t('isRequired'),
]"
/>

<v-select
clearable
v-if="v.type === 'enum'"
v-else-if="v.type === 'enum'"
:label="v.title + (v.required ? ' *' : '')"
:hint="v.description"
v-model="editedEnvironment[v.name]"
Expand All @@ -60,6 +73,7 @@
item-text="name"
item-value="value"
/>

<v-text-field
v-else
:label="v.title + (v.required ? ' *' : '')"
Expand Down Expand Up @@ -169,6 +183,7 @@ export default {
buildTasks: null,
commitAvailable: null,
editedEnvironment: null,
editedSecretEnvironment: null,
cmOptions: {
tabSize: 2,
mode: 'application/json',
Expand Down Expand Up @@ -228,6 +243,7 @@ export default {
});
this.editedEnvironment = JSON.parse(v.environment || '{}');
this.editedSecretEnvironment = JSON.parse(v.secret || '{}');
this.commitAvailable = v.commit_hash != null;
},
Expand All @@ -239,6 +255,7 @@ export default {
beforeSave() {
this.item.environment = JSON.stringify(this.editedEnvironment);
this.item.secret = JSON.stringify(this.editedSecretEnvironment);
},
async afterLoadData() {
Expand Down

0 comments on commit 22ed357

Please sign in to comment.