-
Notifications
You must be signed in to change notification settings - Fork 18
/
job.go
141 lines (125 loc) · 3.63 KB
/
job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cli
import (
"context"
"fmt"
"github.com/MakeNowJust/heredoc"
"github.com/go-playground/validator/v10"
"github.com/raystack/guardian/internal/server"
"github.com/raystack/guardian/jobs"
"github.com/raystack/guardian/pkg/crypto"
"github.com/raystack/guardian/plugins/notifiers"
"github.com/raystack/salt/log"
"github.com/spf13/cobra"
)
func JobCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "job",
Aliases: []string{"jobs"},
Short: "Manage jobs",
Example: heredoc.Doc(`
$ guardian job run fetch_resources
`),
}
cmd.AddCommand(
runJobCmd(),
)
cmd.PersistentFlags().StringP("config", "c", "./config.yaml", "Config file path")
cmd.MarkPersistentFlagFilename("config")
return cmd
}
func runJobCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "Fire a specific job",
Example: heredoc.Doc(`
$ guardian job run fetch_resources
$ guardian job run expiring_grant_notification
$ guardian job run revoke_expired_grants
$ guardian job run revoke_grants_by_user_criteria
`),
Args: cobra.ExactValidArgs(1),
ValidArgs: []string{
string(jobs.TypeFetchResources),
string(jobs.TypeExpiringGrantNotification),
string(jobs.TypeRevokeExpiredGrants),
string(jobs.TypeRevokeGrantsByUserCriteria),
string(jobs.TypeRevokeExpiredAccess),
string(jobs.TypeExpiringAccessNotification),
},
RunE: func(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("getting config flag value: %w", err)
}
config, err := server.LoadConfig(configFile)
if err != nil {
return fmt.Errorf("loading config: %w", err)
}
logger := log.NewLogrus(log.LogrusWithLevel(config.LogLevel))
crypto := crypto.NewAES(config.EncryptionSecretKeyKey)
validator := validator.New()
notifier, err := notifiers.NewClient(&config.Notifier)
if err != nil {
return err
}
services, err := server.InitServices(server.ServiceDeps{
Config: &config,
Logger: logger,
Validator: validator,
Notifier: notifier,
Crypto: crypto,
})
if err != nil {
return fmt.Errorf("initializing services: %w", err)
}
handler := jobs.NewHandler(
logger,
services.GrantService,
services.ProviderService,
notifier,
crypto,
validator,
)
jobsMap := map[jobs.Type]*struct {
handler func(context.Context, jobs.Config) error
config jobs.Config
}{
jobs.TypeFetchResources: {
handler: handler.FetchResources,
config: config.Jobs.FetchResources.Config,
},
jobs.TypeExpiringGrantNotification: {
handler: handler.GrantExpirationReminder,
config: config.Jobs.ExpiringGrantNotification.Config,
},
jobs.TypeRevokeExpiredGrants: {
handler: handler.RevokeExpiredGrants,
config: config.Jobs.RevokeExpiredGrants.Config,
},
jobs.TypeRevokeGrantsByUserCriteria: {
handler: handler.RevokeGrantsByUserCriteria,
config: config.Jobs.RevokeGrantsByUserCriteria.Config,
},
// deprecated job names
jobs.TypeExpiringAccessNotification: {
handler: handler.GrantExpirationReminder,
config: config.Jobs.ExpiringAccessNotification.Config,
},
jobs.TypeRevokeExpiredAccess: {
handler: handler.RevokeExpiredGrants,
config: config.Jobs.RevokeExpiredAccess.Config,
},
}
jobName := jobs.Type(args[0])
job := jobsMap[jobName]
if job == nil {
return fmt.Errorf("invalid job name: %s", jobName)
}
if err := job.handler(context.Background(), job.config); err != nil {
return fmt.Errorf(`failed to run job "%s": %w`, jobName, err)
}
return nil
},
}
return cmd
}