-
Notifications
You must be signed in to change notification settings - Fork 59
/
grafana_com_login.go
94 lines (78 loc) · 2.7 KB
/
grafana_com_login.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
package kiosk
import (
"context"
"log"
"os"
"time"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/kb"
)
// GrafanaKioskGCOM creates a chrome-based kiosk using a grafana.com authenticated account.
func GrafanaKioskGCOM(cfg *Config, messages chan string) {
dir, err := os.MkdirTemp(os.TempDir(), "chromedp-kiosk")
if err != nil {
panic(err)
}
log.Println("Using temp dir:", dir)
defer os.RemoveAll(dir)
opts := generateExecutorOptions(dir, cfg)
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
// also set up a custom logger
taskCtx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
defer cancel()
listenChromeEvents(taskCtx, targetCrashed)
// ensure that the browser process is started
if err := chromedp.Run(taskCtx); err != nil {
panic(err)
}
var generatedURL = GenerateURL(cfg.Target.URL, cfg.General.Mode, cfg.General.AutoFit, cfg.Target.IsPlayList)
log.Println("Navigating to ", generatedURL)
/*
Launch chrome, click the grafana.com button, fill out login form and submit
*/
// XPATH of grafana.com login button = //*[@href="login/grafana_com"]/i
// XPATH for grafana.com login (new) = //a[contains(@href,'login/grafana_com')]
// chromedp.WaitVisible(`//*[@href="login/grafana_com"]/i`, chromedp.BySearch),
// Click the grafana_com login button
if err := chromedp.Run(taskCtx,
chromedp.Navigate(generatedURL),
chromedp.ActionFunc(func(context.Context) error {
log.Println("waiting for login dialog")
return nil
}),
chromedp.WaitVisible(`//a[contains(@href,'login/grafana_com')]`, chromedp.BySearch),
chromedp.ActionFunc(func(context.Context) error {
log.Println("gcom login dialog detected")
return nil
}),
chromedp.Click(`//a[contains(@href,'login/grafana_com')]/..`, chromedp.BySearch),
chromedp.ActionFunc(func(context.Context) error {
log.Println("gcom button clicked")
return nil
}),
); err != nil {
panic(err)
}
// Give browser time to load next page (this can be prone to failure, explore different options vs sleeping)
time.Sleep(2000 * time.Millisecond)
// Fill out grafana_com login page
if err := chromedp.Run(taskCtx,
chromedp.WaitVisible(`//input[@name="login"]`, chromedp.BySearch),
chromedp.SendKeys(`//input[@name="login"]`, cfg.Target.Username, chromedp.BySearch),
chromedp.Click(`#submit`, chromedp.ByID),
chromedp.SendKeys(`//input[@name="password"]`, cfg.Target.Password+kb.Enter, chromedp.BySearch),
); err != nil {
panic(err)
}
// blocking wait
for {
messageFromChrome := <-messages
if err := chromedp.Run(taskCtx,
chromedp.Navigate(generatedURL),
); err != nil {
panic(err)
}
log.Println("Chromium output:", messageFromChrome)
}
}