Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge master #59

Merged
merged 4 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- '**'
- '!master'
- 'master'
pull_request:
branches: [ master ]

Expand Down
51 changes: 38 additions & 13 deletions aci-connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"github.com/spf13/viper"
)

const TTLOffset = 120

var responseTimeMetric = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: MetricsPrefix + "response_time_from_apic",
Help: "Histogram of the time (in seconds) each request took to complete.",
Expand All @@ -54,9 +56,10 @@ var refreshFailedMetric = promauto.NewCounterVec(prometheus.CounterOpts{
)

type AciToken struct {
token string
ttl int64
expire int64
token string
ttl int64
expire int64
lifetime int64
}

// AciConnection is the connection object
Expand Down Expand Up @@ -160,34 +163,42 @@ func (c *AciConnection) tokenProcessing() (error, bool) {
if c.token != nil {
c.tokenMutex.Lock()
defer c.tokenMutex.Unlock()
if c.token.expire < time.Now().Unix() {
if c.token.lifetime < time.Now().Unix() {
log.WithFields(log.Fields{
"requestid": c.ctx.Value("requestid"),
"fabric": fmt.Sprintf("%v", c.ctx.Value("fabric")),
"token": fmt.Sprintf("lifetime"),
}).Info("token reached lifetime seconds")
return nil, false
} else if c.token.expire < time.Now().Unix() {
response, status, err := c.get("refresh", fmt.Sprintf("%s%s", c.fabricConfig.Apic[*c.activeController], c.URLMap["refresh"]))
if err != nil || status != 200 {
//errRe = fmt.Errorf("failed to refresh token %s", c.fabricConfig.Apic[*c.activeController])
log.WithFields(log.Fields{
"requestid": c.ctx.Value("requestid"),
"fabric": fmt.Sprintf("%v", c.ctx.Value("fabric")),
"token": fmt.Sprintf("refersh"),
"token": fmt.Sprintf("refresh"),
}).Warning(err)
refreshFailedMetric.With(prometheus.Labels{
"fabric": fmt.Sprintf("%v", c.ctx.Value("fabric"))}).Inc()
return err, false
} else {
c.newToken(response)
c.refreshToken(response)
log.WithFields(log.Fields{
"requestid": c.ctx.Value("requestid"),
"fabric": fmt.Sprintf("%v", c.ctx.Value("fabric")),
"token": fmt.Sprintf("refersh"),
"token": fmt.Sprintf("refresh"),
}).Info("refresh token")
refreshMetric.With(prometheus.Labels{
"fabric": fmt.Sprintf("%v", c.ctx.Value("fabric"))}).Inc()
return nil, true
}
} else {
log.WithFields(log.Fields{
"requestid": c.ctx.Value("requestid"),
"fabric": fmt.Sprintf("%v", c.ctx.Value("fabric")),
"token": fmt.Sprintf("valid"),
"requestid": c.ctx.Value("requestid"),
"fabric": fmt.Sprintf("%v", c.ctx.Value("fabric")),
"token": fmt.Sprintf("valid"),
"valid_time_seconds": c.token.expire - time.Now().Unix(),
}).Info("token still valid")
return nil, true
}
Expand All @@ -199,11 +210,25 @@ func (c *AciConnection) tokenProcessing() (error, bool) {
func (c *AciConnection) newToken(response []byte) {
token := gjson.Get(string(response), "imdata.0.aaaLogin.attributes.token").String()
ttl := gjson.Get(string(response), "imdata.0.aaaLogin.attributes.refreshTimeoutSeconds").Int()
lifetimeSeconds := gjson.Get(string(response), "imdata.0.aaaLogin.attributes.maximumLifetimeSeconds").Int()
now := time.Now().Unix()
c.token = &AciToken{
token: token,
ttl: ttl,
expire: now + ttl - TTLOffset,
lifetime: now + lifetimeSeconds - TTLOffset,
}
}

func (c *AciConnection) refreshToken(response []byte) {
token := gjson.Get(string(response), "imdata.0.aaaLogin.attributes.token").String()
ttl := gjson.Get(string(response), "imdata.0.aaaLogin.attributes.refreshTimeoutSeconds").Int()

c.token = &AciToken{
token: token,
ttl: ttl,
expire: time.Now().Unix() + ttl - 60,
token: token,
ttl: ttl,
expire: time.Now().Unix() + ttl - TTLOffset,
lifetime: c.token.lifetime,
}
}

Expand Down
Loading