Skip to content

Commit e5bfd48

Browse files
committed
improve auth handling
Signed-off-by: Markus Blaschke <[email protected]>
1 parent 3c34235 commit e5bfd48

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

shellyplug/prober.gen1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func (sp *ShellyPlug) collectFromTargetGen1(target discovery.DiscoveryTarget, logger *zap.SugaredLogger, infoLabels, targetLabels prometheus.Labels) {
1414
shellyProber := shellyprober.ShellyProberGen1{
1515
Target: target,
16-
Client: sp.client.Clone(),
16+
Client: sp.cloneRestyClient(),
1717
Ctx: sp.ctx,
1818
Cache: globalCache,
1919
}

shellyplug/prober.gen2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (sp *ShellyPlug) collectFromTargetGen2(target discovery.DiscoveryTarget, lo
2424

2525
shellyProber := shellyprober.ShellyProberGen2{
2626
Target: target,
27-
Client: sp.client.Clone(),
27+
Client: sp.cloneRestyClient(),
2828
Ctx: sp.ctx,
2929
Cache: globalCache,
3030
}

shellyplug/prober.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ type (
2727
logger *zap.SugaredLogger
2828
registry *prometheus.Registry
2929

30+
auth struct {
31+
username string
32+
password string
33+
}
34+
3035
targets struct {
3136
list []discovery.DiscoveryTarget
3237
lock sync.RWMutex
@@ -80,9 +85,20 @@ func (sp *ShellyPlug) SetTimeout(timeout time.Duration) {
8085
}
8186

8287
func (sp *ShellyPlug) SetHttpAuth(username, password string) {
83-
sp.client.SetDisableWarn(true)
84-
sp.client.SetBasicAuth(username, password)
85-
sp.client.SetDigestAuth(username, password)
88+
sp.auth.username = username
89+
sp.auth.password = password
90+
}
91+
92+
func (sp *ShellyPlug) cloneRestyClient() *resty.Client {
93+
client := sp.client.Clone()
94+
95+
if sp.auth.username != "" {
96+
client.SetDisableWarn(true)
97+
client.SetBasicAuth(sp.auth.username, sp.auth.password)
98+
client.SetDigestAuth(sp.auth.username, sp.auth.password)
99+
}
100+
101+
return client
86102
}
87103

88104
func (sp *ShellyPlug) SetTargets(targets []discovery.DiscoveryTarget) {

0 commit comments

Comments
 (0)