Skip to content

Commit 1590b4e

Browse files
fixed cached service account token (#46)
* fixed cached service account token Signed-off-by: raffaelespazzoli <[email protected]> * incorporated andy's feedback Signed-off-by: raffaelespazzoli <[email protected]> Signed-off-by: raffaelespazzoli <[email protected]>
1 parent a2c00b8 commit 1590b4e

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

controllers/patch_controller.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package controllers
1818

1919
import (
2020
"context"
21+
"os"
22+
"time"
2123

2224
"github.com/redhat-cop/operator-utils/pkg/util"
2325
"github.com/redhat-cop/operator-utils/pkg/util/apis"
@@ -132,11 +134,26 @@ func getJWTToken(context context.Context, serviceAccountName string, kubeNamespa
132134
log := log.FromContext(context)
133135

134136
restConfig := context.Value("restConfig").(*rest.Config)
135-
expiration := int64(600)
137+
lenght, found := os.LookupEnv("SERVICE_ACCOUNT_TOKEN_EXPIRATION_DURATION")
138+
//default is 1 year
139+
defaultDuration, _ := time.ParseDuration("8760h")
140+
var duration time.Duration
141+
if found {
142+
parsedDuration, err := time.ParseDuration(lenght)
143+
if err != nil {
144+
log.Error(err, "unable to parse SERVICE_ACCOUNT_TOKEN_EXPIRATION_DURATION to duration, continuing with", "default duration", defaultDuration)
145+
duration = defaultDuration
146+
} else {
147+
duration = parsedDuration
148+
}
149+
} else {
150+
duration = defaultDuration
151+
}
136152

153+
seconds := int64(duration.Seconds())
137154
treq := &authv1.TokenRequest{
138155
Spec: authv1.TokenRequestSpec{
139-
ExpirationSeconds: &expiration,
156+
ExpirationSeconds: &seconds,
140157
},
141158
}
142159

@@ -153,6 +170,8 @@ func getJWTToken(context context.Context, serviceAccountName string, kubeNamespa
153170
return "", err
154171
}
155172

173+
log.Info("token expiration: " + treq.Status.ExpirationTimestamp.String())
174+
156175
return treq.Status.Token, nil
157176
}
158177

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ The `deployer` service accounts from all namespaces are selected as target of th
384384
### Patch Controller Security Considerations
385385

386386
The patch enforcement enacted by the patch controller is executed with a client which uses the service account referenced by the `serviceAccountRef` field. So before a patch object can actually work an administrator must have granted the needed permissions to a service account in the same namespace. The `serviceAccountRef` will default to the `default` service account if not specified.
387+
This operator uses the TokenRequest API to get a token to instantiate an internal controller to watch for the target(s) and source(s) of a patch. The token request API returns time bound token. At the moment, this token is refreshed when the patch is changed or when the operator is restarted. It is a responsibility of the administrator to make sure that the token is refreshed before its expiration or the patch will stop being enforced. By default, tokens have a 1 year expiration period. This default can be changed via the `SERVICE_ACCOUNT_TOKEN_EXPIRATION_DURATION` environment variable. Environment variables can be set following these [instructions](https://github.com/operator-framework/operator-lifecycle-manager/blob/master/doc/design/subscription-config.md#env) The expected format is of [time.Duration](https://pkg.go.dev/time#ParseDuration)
387388

388389
### Patch Controller Performance Considerations
389390

0 commit comments

Comments
 (0)