restart_action_current_binding_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package api
  2. import (
  3. "context"
  4. "testing"
  5. "connectrpc.com/connect"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. apiv1 "github.com/OliveTin/OliveTin/gen/olivetin/api/v1"
  9. apiv1connect "github.com/OliveTin/OliveTin/gen/olivetin/api/v1/apiv1connect"
  10. config "github.com/OliveTin/OliveTin/internal/config"
  11. "github.com/OliveTin/OliveTin/internal/entities"
  12. "github.com/OliveTin/OliveTin/internal/executor"
  13. )
  14. const restartTestActionID = "restart-test-action"
  15. func restartTestAction(command string, acls ...string) *config.Action {
  16. return &config.Action{
  17. ID: restartTestActionID,
  18. Title: "Restart test action",
  19. Exec: []string{"echo", command},
  20. Acls: acls,
  21. MaxConcurrent: 1,
  22. Timeout: 5,
  23. }
  24. }
  25. func startAndWaitForBinding(t *testing.T, client apiv1connect.OliveTinApiServiceClient, ex *executor.Executor, bindingID string) string {
  26. t.Helper()
  27. resp, err := client.StartAction(context.Background(), connect.NewRequest(&apiv1.StartActionRequest{BindingId: bindingID}))
  28. require.NoError(t, err)
  29. waitForLogFinished(t, ex, resp.Msg.ExecutionTrackingId)
  30. return resp.Msg.ExecutionTrackingId
  31. }
  32. func restartAndWait(t *testing.T, client apiv1connect.OliveTinApiServiceClient, ex *executor.Executor, trackingID string) *executor.InternalLogEntry {
  33. t.Helper()
  34. resp, err := client.RestartAction(context.Background(), connect.NewRequest(&apiv1.RestartActionRequest{ExecutionTrackingId: trackingID}))
  35. require.NoError(t, err)
  36. waitForLogFinished(t, ex, resp.Msg.ExecutionTrackingId)
  37. entry, ok := ex.GetLog(resp.Msg.ExecutionTrackingId)
  38. require.True(t, ok)
  39. return entry
  40. }
  41. func requireRestartNotFound(t *testing.T, client apiv1connect.OliveTinApiServiceClient, trackingID string) {
  42. t.Helper()
  43. _, err := client.RestartAction(context.Background(), connect.NewRequest(&apiv1.RestartActionRequest{ExecutionTrackingId: trackingID}))
  44. require.Error(t, err)
  45. assert.Equal(t, connect.CodeNotFound, connect.CodeOf(err))
  46. }
  47. func logCount(ex *executor.Executor) int {
  48. logs, _ := ex.GetLogTrackingIds(0, 1000)
  49. return len(logs)
  50. }
  51. func entityBindingID(ex *executor.Executor) string {
  52. ex.MapActionBindingsLock.RLock()
  53. defer ex.MapActionBindingsLock.RUnlock()
  54. for id, binding := range ex.MapActionBindings {
  55. if binding.Entity != nil {
  56. return id
  57. }
  58. }
  59. return ""
  60. }
  61. func newRestartTestServer(t *testing.T, cfg *config.Config) (*executor.Executor, apiv1connect.OliveTinApiServiceClient) {
  62. t.Helper()
  63. ex := executor.DefaultExecutor(cfg)
  64. ex.RebuildActionMap()
  65. ts, client := getNewTestServerAndClientWithExecutor(cfg, ex)
  66. t.Cleanup(ts.Close)
  67. return ex, client
  68. }
  69. func TestRestartActionRunsUnchangedActionAgain(t *testing.T) {
  70. cfg := config.DefaultConfig()
  71. cfg.Actions = []*config.Action{restartTestAction("first-run")}
  72. ex, client := newRestartTestServer(t, cfg)
  73. trackingID := startAndWaitForBinding(t, client, ex, restartTestActionID)
  74. ex.RebuildActionMap()
  75. entry := restartAndWait(t, client, ex, trackingID)
  76. assert.False(t, entry.Blocked)
  77. assert.Contains(t, entry.Output, "first-run")
  78. }
  79. func TestRestartActionRejectsActionRemovedFromConfig(t *testing.T) {
  80. cfg := config.DefaultConfig()
  81. cfg.Actions = []*config.Action{restartTestAction("removed-command")}
  82. ex, client := newRestartTestServer(t, cfg)
  83. trackingID := startAndWaitForBinding(t, client, ex, restartTestActionID)
  84. logsBefore := logCount(ex)
  85. cfg.Actions = nil
  86. ex.RebuildActionMap()
  87. requireRestartNotFound(t, client, trackingID)
  88. assert.Equal(t, logsBefore, logCount(ex), "a rejected restart must not start an execution")
  89. }
  90. func TestRestartActionUsesCurrentAclsAfterReload(t *testing.T) {
  91. cfg := config.DefaultConfig()
  92. cfg.DefaultPermissions.Exec = false
  93. cfg.AccessControlLists = []*config.AccessControlList{
  94. {Name: "guest-exec", MatchUsernames: []string{"guest"}, Permissions: config.PermissionsList{Exec: true}},
  95. {Name: "admin-exec", MatchUsernames: []string{"admin"}, Permissions: config.PermissionsList{Exec: true}},
  96. }
  97. cfg.Actions = []*config.Action{restartTestAction("old-guest-command", "guest-exec")}
  98. ex, client := newRestartTestServer(t, cfg)
  99. trackingID := startAndWaitForBinding(t, client, ex, restartTestActionID)
  100. cfg.Actions = []*config.Action{restartTestAction("new-admin-command", "admin-exec")}
  101. ex.RebuildActionMap()
  102. entry := restartAndWait(t, client, ex, trackingID)
  103. assert.True(t, entry.Blocked, "the guest must be blocked by the action's current ACLs")
  104. assert.NotContains(t, entry.Output, "old-guest-command")
  105. assert.NotContains(t, entry.Output, "new-admin-command")
  106. }
  107. func TestRestartActionRunsCurrentCommandAfterReload(t *testing.T) {
  108. cfg := config.DefaultConfig()
  109. cfg.Actions = []*config.Action{restartTestAction("old-command")}
  110. ex, client := newRestartTestServer(t, cfg)
  111. trackingID := startAndWaitForBinding(t, client, ex, restartTestActionID)
  112. cfg.Actions = []*config.Action{restartTestAction("new-command")}
  113. ex.RebuildActionMap()
  114. entry := restartAndWait(t, client, ex, trackingID)
  115. assert.False(t, entry.Blocked)
  116. assert.Contains(t, entry.Output, "new-command")
  117. assert.NotContains(t, entry.Output, "old-command")
  118. }
  119. func TestRestartActionRequiresJustificationAddedAfterReload(t *testing.T) {
  120. cfg := config.DefaultConfig()
  121. cfg.Actions = []*config.Action{restartTestAction("no-justification")}
  122. ex, client := newRestartTestServer(t, cfg)
  123. trackingID := startAndWaitForBinding(t, client, ex, restartTestActionID)
  124. action := restartTestAction("no-justification")
  125. action.Justification = config.JustificationRequiredNoTemplate
  126. cfg.Actions = []*config.Action{action}
  127. ex.RebuildActionMap()
  128. _, err := client.RestartAction(context.Background(), connect.NewRequest(&apiv1.RestartActionRequest{ExecutionTrackingId: trackingID}))
  129. require.Error(t, err)
  130. assert.Contains(t, err.Error(), "justification")
  131. }
  132. func TestRestartActionRequiresArgumentAddedAfterReload(t *testing.T) {
  133. cfg := config.DefaultConfig()
  134. cfg.Actions = []*config.Action{restartTestAction("no-arguments")}
  135. ex, client := newRestartTestServer(t, cfg)
  136. trackingID := startAndWaitForBinding(t, client, ex, restartTestActionID)
  137. action := restartTestAction("{{ host }}")
  138. action.Arguments = []config.ActionArgument{{Name: "host", Type: "ascii_identifier"}}
  139. cfg.Actions = []*config.Action{action}
  140. ex.RebuildActionMap()
  141. _, err := client.RestartAction(context.Background(), connect.NewRequest(&apiv1.RestartActionRequest{ExecutionTrackingId: trackingID}))
  142. require.Error(t, err)
  143. assert.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))
  144. }
  145. func TestRestartActionRejectsEntityRemovedAfterReload(t *testing.T) {
  146. const entityType = "restarttestservers"
  147. entities.ClearEntitiesOfType(entityType)
  148. t.Cleanup(func() { entities.ClearEntitiesOfType(entityType) })
  149. entities.AddEntity(entityType, "web01", map[string]any{"name": "web01"})
  150. cfg := config.DefaultConfig()
  151. cfg.Actions = []*config.Action{{
  152. Title: "Restart entity test",
  153. Exec: []string{"echo", "entity-command"},
  154. Entity: entityType,
  155. MaxConcurrent: 1,
  156. Timeout: 5,
  157. }}
  158. ex, client := newRestartTestServer(t, cfg)
  159. bindingID := entityBindingID(ex)
  160. require.NotEmpty(t, bindingID)
  161. trackingID := startAndWaitForBinding(t, client, ex, bindingID)
  162. entities.ClearEntitiesOfType(entityType)
  163. ex.RebuildActionMap()
  164. requireRestartNotFound(t, client, trackingID)
  165. }