|
@@ -21,7 +21,7 @@ func TestStartActionRequiresJustificationForGuest(t *testing.T) {
|
|
|
action := &config.Action{
|
|
action := &config.Action{
|
|
|
Title: "Send email",
|
|
Title: "Send email",
|
|
|
ID: "send_email",
|
|
ID: "send_email",
|
|
|
- Justification: true,
|
|
|
|
|
|
|
+ Justification: config.JustificationRequiredNoTemplate,
|
|
|
Shell: "echo done",
|
|
Shell: "echo done",
|
|
|
}
|
|
}
|
|
|
cfg.Actions = append(cfg.Actions, action)
|
|
cfg.Actions = append(cfg.Actions, action)
|
|
@@ -56,12 +56,12 @@ func TestStartActionRequiresJustificationForGuest(t *testing.T) {
|
|
|
assert.Equal(t, "New user registration foo@example.com", entry.Justification)
|
|
assert.Equal(t, "New user registration foo@example.com", entry.Justification)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func TestBuildActionExposesJustificationFlag(t *testing.T) {
|
|
|
|
|
|
|
+func TestBuildActionExposesJustificationTemplate(t *testing.T) {
|
|
|
cfg := config.DefaultConfig()
|
|
cfg := config.DefaultConfig()
|
|
|
action := &config.Action{
|
|
action := &config.Action{
|
|
|
Title: "Audited action",
|
|
Title: "Audited action",
|
|
|
ID: "audited",
|
|
ID: "audited",
|
|
|
- Justification: true,
|
|
|
|
|
|
|
+ Justification: "{{ target }}",
|
|
|
Shell: "echo hi",
|
|
Shell: "echo hi",
|
|
|
}
|
|
}
|
|
|
cfg.Actions = append(cfg.Actions, action)
|
|
cfg.Actions = append(cfg.Actions, action)
|
|
@@ -77,12 +77,98 @@ func TestBuildActionExposesJustificationFlag(t *testing.T) {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
require.NotNil(t, pb)
|
|
require.NotNil(t, pb)
|
|
|
- assert.True(t, pb.Justification)
|
|
|
|
|
|
|
+ assert.Equal(t, "{{ target }}", pb.Justification)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func TestBuildActionExposesBlankRequiredJustification(t *testing.T) {
|
|
|
|
|
+ cfg := config.DefaultConfig()
|
|
|
|
|
+ action := &config.Action{
|
|
|
|
|
+ Title: "Audited action",
|
|
|
|
|
+ ID: "audited",
|
|
|
|
|
+ Justification: config.JustificationRequiredNoTemplate,
|
|
|
|
|
+ Shell: "echo hi",
|
|
|
|
|
+ }
|
|
|
|
|
+ cfg.Actions = append(cfg.Actions, action)
|
|
|
|
|
+
|
|
|
|
|
+ ex := executor.DefaultExecutor(cfg)
|
|
|
|
|
+ ex.RebuildActionMap()
|
|
|
|
|
+ binding := ex.FindBindingWithNoEntity(action)
|
|
|
|
|
+ require.NotNil(t, binding)
|
|
|
|
|
+
|
|
|
|
|
+ pb := buildAction(binding, &DashboardRenderRequest{
|
|
|
|
|
+ cfg: cfg,
|
|
|
|
|
+ ex: ex,
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ require.NotNil(t, pb)
|
|
|
|
|
+ assert.Equal(t, config.JustificationRequiredNoTemplate, pb.Justification)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func TestResolveStartJustificationUsesTemplateWhenClientValueEmpty(t *testing.T) {
|
|
|
|
|
+ action := &config.Action{
|
|
|
|
|
+ Justification: "{{ ansible_host }}",
|
|
|
|
|
+ }
|
|
|
|
|
+ binding := &executor.ActionBinding{}
|
|
|
|
|
+
|
|
|
|
|
+ got := resolveStartJustification(action, binding, "", map[string]string{
|
|
|
|
|
+ "ansible_host": "192.168.66.8",
|
|
|
|
|
+ })
|
|
|
|
|
+ assert.Equal(t, "192.168.66.8", got)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func TestResolveStartJustificationPrefersClientValue(t *testing.T) {
|
|
|
|
|
+ action := &config.Action{
|
|
|
|
|
+ Justification: "{{ ansible_host }}",
|
|
|
|
|
+ }
|
|
|
|
|
+ binding := &executor.ActionBinding{}
|
|
|
|
|
+
|
|
|
|
|
+ got := resolveStartJustification(action, binding, "manual reason", map[string]string{
|
|
|
|
|
+ "ansible_host": "192.168.66.8",
|
|
|
|
|
+ })
|
|
|
|
|
+ assert.Equal(t, "manual reason", got)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func TestStartActionResolvesJustificationTemplateForGuest(t *testing.T) {
|
|
|
|
|
+ cfg := config.DefaultConfig()
|
|
|
|
|
+ action := &config.Action{
|
|
|
|
|
+ Title: "Run playbook",
|
|
|
|
|
+ ID: "run_playbook",
|
|
|
|
|
+ Justification: "{{ ansible_host }}",
|
|
|
|
|
+ Shell: "echo done",
|
|
|
|
|
+ Arguments: []config.ActionArgument{
|
|
|
|
|
+ {Name: "ansible_host", Title: "Host"},
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+ cfg.Actions = append(cfg.Actions, action)
|
|
|
|
|
+
|
|
|
|
|
+ ex := executor.DefaultExecutor(cfg)
|
|
|
|
|
+ ex.RebuildActionMap()
|
|
|
|
|
+ binding := ex.FindBindingWithNoEntity(action)
|
|
|
|
|
+ require.NotNil(t, binding)
|
|
|
|
|
+
|
|
|
|
|
+ ts, client := getNewTestServerAndClientWithExecutor(cfg, ex)
|
|
|
|
|
+ defer ts.Close()
|
|
|
|
|
+
|
|
|
|
|
+ resp, err := client.StartAction(context.Background(), connect.NewRequest(&apiv1.StartActionRequest{
|
|
|
|
|
+ BindingId: binding.ID,
|
|
|
|
|
+ UniqueTrackingId: uuid.NewString(),
|
|
|
|
|
+ Arguments: []*apiv1.StartActionArgument{
|
|
|
|
|
+ {Name: "ansible_host", Value: "stuffbox"},
|
|
|
|
|
+ },
|
|
|
|
|
+ }))
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ require.NotEmpty(t, resp.Msg.ExecutionTrackingId)
|
|
|
|
|
+
|
|
|
|
|
+ time.Sleep(200 * time.Millisecond)
|
|
|
|
|
+
|
|
|
|
|
+ entry, ok := ex.GetLog(resp.Msg.ExecutionTrackingId)
|
|
|
|
|
+ require.True(t, ok)
|
|
|
|
|
+ assert.Equal(t, "stuffbox", entry.Justification)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func TestValidateJustificationRequiredAllowsSystemUser(t *testing.T) {
|
|
func TestValidateJustificationRequiredAllowsSystemUser(t *testing.T) {
|
|
|
cfg := config.DefaultConfig()
|
|
cfg := config.DefaultConfig()
|
|
|
- action := &config.Action{Title: "Cron job", Justification: true}
|
|
|
|
|
|
|
+ action := &config.Action{Title: "Cron job", Justification: config.JustificationRequiredNoTemplate}
|
|
|
|
|
|
|
|
err := validateJustificationRequired(action, "", auth.UserFromSystem(cfg, "cron"))
|
|
err := validateJustificationRequired(action, "", auth.UserFromSystem(cfg, "cron"))
|
|
|
require.NoError(t, err)
|
|
require.NoError(t, err)
|