Skip to content

Commit ad4b1bc

Browse files
Anthony Landrethtony-landreth
Anthony Landreth
authored andcommitted
Adds registration to unencumber PGO-431
1 parent 8385a5a commit ad4b1bc

File tree

14 files changed

+389
-63
lines changed

14 files changed

+389
-63
lines changed

cmd/postgres-operator/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,15 @@ func main() {
130130
func addControllersToManager(mgr manager.Manager, openshift bool, log logr.Logger) {
131131
pgReconciler := &postgrescluster.Reconciler{
132132
Client: mgr.GetClient(),
133+
IsOpenShift: openshift,
133134
Owner: postgrescluster.ControllerName,
135+
PGOVersion: versionString,
134136
Recorder: mgr.GetEventRecorderFor(postgrescluster.ControllerName),
135-
Tracer: otel.Tracer(postgrescluster.ControllerName),
136-
IsOpenShift: openshift,
137+
// TODO(tlandreth) Replace the contents of cpk_rsa_key.pub with a key from a
138+
// Crunchy authorization server.
139+
Registration: util.GetRegistration(os.Getenv("RSA_KEY"), os.Getenv("TOKEN_PATH"), log),
140+
RegistrationURL: os.Getenv("REGISTRATION_URL"),
141+
Tracer: otel.Tracer(postgrescluster.ControllerName),
137142
}
138143

139144
if err := pgReconciler.SetupWithManager(mgr); err != nil {

cpk_rsa_key.pub

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu1SU1LfVLPHCozMxH2Mo
3+
4lgOEePzNm0tRgeLezV6ffAt0gunVTLw7onLRnrq0/IzW7yWR7QkrmBL7jTKEn5u
4+
+qKhbwKfBstIs+bMY2Zkp18gnTxKLxoS2tFczGkPLPgizskuemMghRniWaoLcyeh
5+
kd3qqGElvW/VDL5AaWTg0nLVkjRo9z+40RQzuVaE8AkAFmxZzow3x+VJYKdjykkJ
6+
0iT9wCS0DRTXu269V264Vf/3jvredZiKRkgwlL9xNAwxXFg0x/XFw005UWVRIkdg
7+
cKWTjpBP2dPwVZ4WWC+9aGVd+Gyn1o0CLelf4rEjGoXbAAEgAqeGUxrcIlbjXfbc
8+
mwIDAQAB
9+
-----END PUBLIC KEY-----

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.19
55
require (
66
github.com/evanphx/json-patch/v5 v5.6.0
77
github.com/go-logr/logr v1.2.2
8+
github.com/golang-jwt/jwt/v5 v5.0.0
89
github.com/google/go-cmp v0.5.9
910
github.com/google/uuid v1.3.0
1011
github.com/onsi/ginkgo/v2 v2.0.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zV
189189
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
190190
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
191191
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
192+
github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE=
193+
github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
192194
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
193195
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
194196
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=

internal/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ func RegistrationRequired() bool {
3636

3737
// Get the version of CPK that applied the first RegistrationRequired status to this cluster.
3838
func RegistrationRequiredBy(cluster *v1beta1.PostgresCluster) string {
39+
if cluster.Status.RegistrationRequired == nil {
40+
return ""
41+
}
3942
return cluster.Status.RegistrationRequired.PGOVersion
4043
}
4144

internal/controller/postgrescluster/controller.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io"
2222
"os"
2323
"strconv"
24+
"time"
2425

2526
"github.com/pkg/errors"
2627
"go.opentelemetry.io/otel/trace"
@@ -50,6 +51,7 @@ import (
5051
"github.com/crunchydata/postgres-operator/internal/pgmonitor"
5152
"github.com/crunchydata/postgres-operator/internal/pki"
5253
"github.com/crunchydata/postgres-operator/internal/postgres"
54+
"github.com/crunchydata/postgres-operator/internal/util"
5355
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
5456
)
5557

@@ -61,15 +63,17 @@ const (
6163
// Reconciler holds resources for the PostgresCluster reconciler
6264
type Reconciler struct {
6365
Client client.Client
64-
Owner client.FieldOwner
65-
Recorder record.EventRecorder
66-
Tracer trace.Tracer
6766
IsOpenShift bool
68-
69-
PodExec func(
67+
Owner client.FieldOwner
68+
PGOVersion string
69+
PodExec func(
7070
namespace, pod, container string,
7171
stdin io.Reader, stdout, stderr io.Writer, command ...string,
7272
) error
73+
Recorder record.EventRecorder
74+
Registration util.Registration
75+
RegistrationURL string
76+
Tracer trace.Tracer
7377
}
7478

7579
// +kubebuilder:rbac:groups="",resources="events",verbs={create,patch}
@@ -209,13 +213,17 @@ func (r *Reconciler) Reconcile(
209213
return result, err
210214
}
211215

212-
if config.RegistrationRequired() {
216+
if config.RegistrationRequired() && !r.registrationValid() {
213217
if !registrationRequiredStatusFound(cluster) {
214-
addRegistrationRequiredStatus(cluster)
218+
addRegistrationRequiredStatus(cluster, r.PGOVersion)
215219
return patchClusterStatus()
216220
}
217221

218-
if shouldEncumberReconciliation(cluster) {
222+
if r.tokenAuthenticationFailed() {
223+
r.Recorder.Event(cluster, corev1.EventTypeWarning, "Token Authentication Failed", "See "+r.RegistrationURL+" for details.")
224+
}
225+
226+
if shouldEncumberReconciliation(r.Registration.Authenticated, cluster, r.PGOVersion) {
219227
emitEncumbranceWarning(cluster, r)
220228
// Encumbrance is just an early return from the reconciliation loop.
221229
return patchClusterStatus()
@@ -224,6 +232,17 @@ func (r *Reconciler) Reconcile(
224232
}
225233
}
226234

235+
if config.RegistrationRequired() && r.registrationValid() {
236+
if tokenRequiredConditionFound(cluster) {
237+
meta.RemoveStatusCondition(&cluster.Status.Conditions, v1beta1.TokenRequired)
238+
}
239+
240+
if registrationRequiredStatusFound(cluster) {
241+
cluster.Status.RegistrationRequired = nil
242+
r.Recorder.Event(cluster, corev1.EventTypeNormal, "Token Verified", "Thank you for registering your installation of Crunchy Postgres for Kubernetes.")
243+
}
244+
}
245+
227246
// if the cluster is paused, set a condition and return
228247
if cluster.Spec.Paused != nil && *cluster.Spec.Paused {
229248
meta.SetStatusCondition(&cluster.Status.Conditions, metav1.Condition{
@@ -390,6 +409,20 @@ func (r *Reconciler) Reconcile(
390409
return patchClusterStatus()
391410
}
392411

412+
func (r *Reconciler) tokenAuthenticationFailed() bool {
413+
return r.Registration.TokenFileFound && r.Registration.Authenticated
414+
}
415+
416+
func (r *Reconciler) registrationValid() bool {
417+
expiry := r.Registration.Exp
418+
authenticated := r.Registration.Authenticated
419+
// Use epoch time in seconds, consistent with RFC 7519.
420+
now := time.Now().Unix()
421+
expired := expiry < now
422+
423+
return authenticated && !expired
424+
}
425+
393426
// deleteControlled safely deletes object when it is controlled by cluster.
394427
func (r *Reconciler) deleteControlled(
395428
ctx context.Context, cluster *v1beta1.PostgresCluster, object client.Object,

0 commit comments

Comments
 (0)