grafana/apps/provisioning/pkg/connection/github/extra.go
Roberto Jiménez Sánchez 8e610d4619
Provisioning: add Validate to Extra interfaces and move connection runtime validation to controller health checks (#116395)
* feat: add Validate to Extra interfaces for repositories and connections

* chore: remove accidentally committed pkg/extensions validator files (should be git ignored)

* Do not duplicate git checks

* Fix some tests

* Format fmt

* Keep coverage in validator test for local

* Do not build for validation in register

* consolidate copy of secure values

* Use copy function in test endpoint

* Call Factory validate in validator

* Add Test method for Connection

* Change the signature of Validate to return ErrorFields

* Health check for connection in controller

* Fix linting

* Fix static check

* Fix enterprise tests
2026-01-16 16:52:58 +01:00

71 lines
1.6 KiB
Go

package github
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/grafana/grafana-app-sdk/logging"
provisioning "github.com/grafana/grafana/apps/provisioning/pkg/apis/provisioning/v0alpha1"
"github.com/grafana/grafana/apps/provisioning/pkg/connection"
)
type extra struct {
factory GithubFactory
decrypter connection.Decrypter
}
func (e *extra) Type() provisioning.ConnectionType {
return provisioning.GithubConnectionType
}
func (e *extra) Build(ctx context.Context, conn *provisioning.Connection) (connection.Connection, error) {
logger := logging.FromContext(ctx)
if conn == nil || conn.Spec.GitHub == nil {
logger.Error("connection is nil or github info is nil")
return nil, fmt.Errorf("invalid github connection")
}
// Decrypt secure values
secure := e.decrypter(conn)
// Decrypt private key
pKey, err := secure.PrivateKey(ctx)
if err != nil {
logger.Error("Failed to decrypt private key", "error", err)
return nil, err
}
// Decrypt token
t, err := secure.Token(ctx)
if err != nil {
logger.Error("Failed to decrypt token", "error", err)
return nil, err
}
c := NewConnection(conn, e.factory, ConnectionSecrets{
PrivateKey: pKey,
Token: t,
})
return &c, nil
}
func (e *extra) Mutate(ctx context.Context, obj runtime.Object) error {
return Mutate(ctx, obj)
}
func (e *extra) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
return Validate(ctx, obj)
}
func Extra(decrypter connection.Decrypter, factory GithubFactory) connection.Extra {
return &extra{
decrypter: decrypter,
factory: factory,
}
}