@@ -15,6 +15,7 @@ import (
1515 "errors"
1616 "fmt"
1717 "log"
18+ "net/url"
1819 "os"
1920 "path/filepath"
2021 "sync"
@@ -28,26 +29,35 @@ import (
2829// ------------------------------------------------------------------ config ---
2930
3031type Config struct {
31- DebugEnabled bool
32- AwsConsoleURL string
33- SlackToken string
34- SlackChannel string
32+ DebugEnabled bool
33+ AwsAccessPortalURL string
34+ AwsAccessRoleName string
35+ AwsConsoleURL string
36+ SlackToken string
37+ SlackChannel string
3538}
3639
3740func BuildConfig () (Config , error ) {
3841 cfg := Config {
39- DebugEnabled : os .Getenv ("APP_DEBUG_ENABLED" ) == "true" ,
40- AwsConsoleURL : os .Getenv ("APP_AWS_CONSOLE_URL" ),
41- SlackToken : os .Getenv ("APP_SLACK_TOKEN" ),
42- SlackChannel : os .Getenv ("APP_SLACK_CHANNEL" ),
42+ DebugEnabled : os .Getenv ("APP_DEBUG_ENABLED" ) == "true" ,
43+ AwsConsoleURL : os .Getenv ("APP_AWS_CONSOLE_URL" ),
44+ AwsAccessPortalURL : os .Getenv ("APP_AWS_ACCESS_PORTAL_URL" ),
45+ AwsAccessRoleName : os .Getenv ("APP_AWS_ACCESS_ROLE_NAME" ),
46+ SlackToken : os .Getenv ("APP_SLACK_TOKEN" ),
47+ SlackChannel : os .Getenv ("APP_SLACK_CHANNEL" ),
4348 }
4449 switch {
4550 case cfg .SlackToken == "" :
4651 return Config {}, errors .New ("missing env var APP_SLACK_TOKEN" )
4752 case cfg .SlackChannel == "" :
4853 return Config {}, errors .New ("missing env var APP_SLACK_CHANNEL" )
49- case cfg .AwsConsoleURL == "" :
50- return Config {}, errors .New ("missing env var APP_AWS_CONSOLE_URL" )
54+ case cfg .AwsAccessPortalURL == "" :
55+ return Config {}, errors .New ("missing env var APP_AWS_ACCESS_PORTAL_URL" )
56+ case cfg .AwsAccessRoleName == "" :
57+ return Config {}, errors .New ("missing env var APP_AWS_ACCESS_ROLE_NAME" )
58+ }
59+ if cfg .AwsConsoleURL == "" {
60+ cfg .AwsConsoleURL = "https://console.aws.amazon.com"
5161 }
5262 return cfg , nil
5363}
@@ -66,22 +76,33 @@ func NewApp(cfg Config) *App {
6676 }
6777}
6878
69- func (a * App ) ParseFindingData (raw json.RawMessage ) (Finding , error ) {
79+ // https://mytech.awsapps.com/start/#/console?account_id=883776786067&role_name=AdministratorAccess
80+ // https://console.aws.amazon.com/guardduty/home?region=us-east-1#/findings?macros=current&fId=5ecc8a2d96f2c23bde37397e4cec0cd0
81+ func (a * App ) BuildConsoleURL (gdAccountId string , f * Finding ) string {
82+ dst := fmt .Sprintf (
83+ "%s/guardduty/home?region=%s#/findings?¯os=current&fId=%s" ,
84+ a .cfg .AwsConsoleURL , f .Region , f .ID ,
85+ )
86+ dstEncoded := url .QueryEscape (dst )
87+ return fmt .Sprintf (
88+ "%s/#/console?account_id=%s&role_name=%s&destination=%s" ,
89+ a .cfg .AwsAccessPortalURL , gdAccountId , a .cfg .AwsAccessRoleName , dstEncoded ,
90+ )
91+ }
92+
93+ func (a * App ) ParseFindingData (gdAccountId string , raw json.RawMessage ) (Finding , error ) {
7094 var f Finding
7195 if err := json .Unmarshal (raw , & f ); err != nil {
7296 return Finding {}, err
7397 }
74- f .ConsoleURL = fmt .Sprintf (
75- "%s/guardduty/home?region=%s#/findings?¯os=current&fId=%s" ,
76- a .cfg .AwsConsoleURL , f .Region , f .ID ,
77- )
98+ f .ConsoleURL = a .BuildConsoleURL (gdAccountId , & f )
7899 f .Raw = raw
79100 f .SeverityLabel = f .ToSeverityLevel ()
80101 return f , nil
81102}
82103
83- func (a * App ) Process (raw json.RawMessage ) error {
84- f , err := a .ParseFindingData (raw )
104+ func (a * App ) Process (gdAccountId string , raw json.RawMessage ) error {
105+ f , err := a .ParseFindingData (gdAccountId , raw )
85106 if err != nil {
86107 return err
87108 }
@@ -187,7 +208,7 @@ func LambdaHandler(_ context.Context, evt events.CloudWatchEvent) error {
187208 }
188209 log .Print (string (evtJson ))
189210
190- return app .Process (evt .Detail )
211+ return app .Process (evt .AccountID , evt . Detail )
191212}
192213
193214// ------------------------------------------------------------- cmd: sample ---
@@ -220,7 +241,7 @@ func ProcessSamples(a *App) error {
220241 }
221242
222243 for _ , e := range events {
223- if err := a .Process (e .Detail ); err != nil {
244+ if err := a .Process (e .AccountID , e . Detail ); err != nil {
224245 return fmt .Errorf ("process id=%s: %w" , e .ID , err )
225246 }
226247 }
0 commit comments