ShortURL: App platform migration support for dual write (#109221)
This commit is contained in:
@@ -3,6 +3,7 @@ package kinds
|
||||
shorturl: {
|
||||
kind: "ShortURL"
|
||||
pluralName: "ShortURLs"
|
||||
validation: operations: ["CREATE","UPDATE"]
|
||||
schema: {
|
||||
spec: {
|
||||
// The original path to where the short url is linking too e.g. https://localhost:3000/eer8i1kictngga/new-dashboard-with-lib-panel
|
||||
|
||||
@@ -35,7 +35,15 @@ var appManifestData = app.ManifestData{
|
||||
Plural: "ShortURLs",
|
||||
Scope: "Namespaced",
|
||||
Conversion: false,
|
||||
Schema: &versionSchemaShortURLv1alpha1,
|
||||
Admission: &app.AdmissionCapabilities{
|
||||
Validation: &app.ValidationCapability{
|
||||
Operations: []app.AdmissionOperation{
|
||||
app.AdmissionOperationCreate,
|
||||
app.AdmissionOperationUpdate,
|
||||
},
|
||||
},
|
||||
},
|
||||
Schema: &versionSchemaShortURLv1alpha1,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -3,6 +3,8 @@ package app
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/klog/v2"
|
||||
@@ -41,6 +43,24 @@ func New(cfg app.Config) (app.App, error) {
|
||||
ManagedKinds: []simple.AppManagedKind{
|
||||
{
|
||||
Kind: shorturlv1alpha1.ShortURLKind(),
|
||||
Validator: &simple.Validator{
|
||||
ValidateFunc: func(ctx context.Context, req *app.AdmissionRequest) error {
|
||||
// Cast the incoming object to ShortURL for validation
|
||||
shortURL, ok := req.Object.(*shorturlv1alpha1.ShortURL)
|
||||
if !ok {
|
||||
return fmt.Errorf("expected ShortURL object, got %T", req.Object)
|
||||
}
|
||||
|
||||
relPath := strings.TrimSpace(shortURL.Spec.Path)
|
||||
if path.IsAbs(relPath) {
|
||||
return fmt.Errorf("%w: %s", ErrShortURLAbsolutePath, relPath)
|
||||
}
|
||||
if strings.Contains(relPath, "../") {
|
||||
return fmt.Errorf("%w: %s", ErrShortURLInvalidPath, relPath)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -7,4 +7,5 @@ type ShortURL struct {
|
||||
|
||||
type CreateShortURLCmd struct {
|
||||
Path string `json:"path"`
|
||||
UID string `json:"uid,omitempty"`
|
||||
}
|
||||
|
||||
+194
-16
@@ -3,46 +3,62 @@ package api
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/teris-io/shortid"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/dynamic"
|
||||
|
||||
"github.com/grafana/grafana/apps/shorturl/pkg/apis/shorturl/v1alpha1"
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
"github.com/grafana/grafana/pkg/registry/apps/shorturl"
|
||||
grafanaapiserver "github.com/grafana/grafana/pkg/services/apiserver"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/services/shorturls"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/grafana/grafana/pkg/util/errhttp"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
func (hs *HTTPServer) registerShortURLAPI(apiRoute routing.RouteRegister) {
|
||||
reqSignedIn := middleware.ReqSignedIn
|
||||
apiRoute.Post("/api/short-urls", reqSignedIn, hs.createShortURL)
|
||||
apiRoute.Get("/goto/:uid", reqSignedIn, hs.redirectFromShortURL, hs.Index)
|
||||
if hs.Features.IsEnabledGlobally(featuremgmt.FlagKubernetesShortURLs) {
|
||||
handler := newShortURLK8sHandler(hs)
|
||||
apiRoute.Post("/api/short-urls", reqSignedIn, handler.createKubernetesShortURLsHandler)
|
||||
apiRoute.Get("/api/short-urls/:uid", reqSignedIn, handler.getKubernetesShortURLsHandler)
|
||||
apiRoute.Get("/goto/:uid", reqSignedIn, handler.getKubernetesRedirectFromShortURL, hs.Index)
|
||||
} else {
|
||||
apiRoute.Post("/api/short-urls", reqSignedIn, hs.createShortURL)
|
||||
apiRoute.Get("/api/short-urls/:uid", reqSignedIn, hs.getShortURL)
|
||||
apiRoute.Get("/goto/:uid", reqSignedIn, hs.redirectFromShortURL, hs.Index)
|
||||
}
|
||||
}
|
||||
|
||||
// createShortURL handles requests to create short URLs.
|
||||
func (hs *HTTPServer) createShortURL(c *contextmodel.ReqContext) response.Response {
|
||||
cmd := dtos.CreateShortURLCmd{}
|
||||
cmd := &dtos.CreateShortURLCmd{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Err(shorturls.ErrShortURLBadRequest.Errorf("bad request data: %w", err))
|
||||
}
|
||||
hs.log.Debug("Received request to create short URL", "path", cmd.Path)
|
||||
shortURL, err := hs.ShortURLService.CreateShortURL(c.Req.Context(), c.SignedInUser, cmd.Path)
|
||||
shortURL, err := hs.ShortURLService.CreateShortURL(c.Req.Context(), c.SignedInUser, cmd)
|
||||
if err != nil {
|
||||
return response.Err(err)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/goto/%s?orgId=%d", strings.TrimSuffix(hs.Cfg.AppURL, "/"), shortURL.Uid, c.GetOrgID())
|
||||
c.Logger.Debug("Created short URL", "url", url)
|
||||
shortURLDTO := hs.ShortURLService.ConvertShortURLToDTO(shortURL, hs.Cfg.AppURL)
|
||||
c.Logger.Debug("Created short URL", "url", shortURLDTO.URL)
|
||||
|
||||
dto := dtos.ShortURL{
|
||||
UID: shortURL.Uid,
|
||||
URL: url,
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, dto)
|
||||
return response.JSON(http.StatusOK, shortURLDTO)
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) redirectFromShortURL(c *contextmodel.ReqContext) {
|
||||
@@ -59,11 +75,11 @@ func (hs *HTTPServer) redirectFromShortURL(c *contextmodel.ReqContext) {
|
||||
// we would try to redirect again.
|
||||
if shorturls.ErrShortURLNotFound.Is(err) {
|
||||
hs.log.Debug("Not redirecting short URL since not found", "uid", shortURLUID)
|
||||
c.Redirect(hs.Cfg.AppURL, 308)
|
||||
c.Redirect(hs.Cfg.AppURL, http.StatusPermanentRedirect)
|
||||
return
|
||||
}
|
||||
hs.log.Error("Short URL redirection error", "err", err)
|
||||
c.Redirect(hs.Cfg.AppURL, 307)
|
||||
c.Redirect(hs.Cfg.AppURL, http.StatusTemporaryRedirect)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -73,5 +89,167 @@ func (hs *HTTPServer) redirectFromShortURL(c *contextmodel.ReqContext) {
|
||||
}
|
||||
|
||||
hs.log.Debug("Redirecting short URL", "path", shortURL.Path)
|
||||
c.Redirect(setting.ToAbsUrl(shortURL.Path), 302)
|
||||
c.Redirect(setting.ToAbsUrl(shortURL.Path), http.StatusFound)
|
||||
}
|
||||
|
||||
// getShortURL handles requests to get short URLs.
|
||||
func (hs *HTTPServer) getShortURL(c *contextmodel.ReqContext) response.Response {
|
||||
shortURLUID := web.Params(c.Req)[":uid"]
|
||||
|
||||
if !util.IsValidShortUID(shortURLUID) {
|
||||
return response.Err(shorturls.ErrShortURLBadRequest.Errorf("invalid uid"))
|
||||
}
|
||||
|
||||
shortURL, err := hs.ShortURLService.GetShortURLByUID(c.Req.Context(), c.SignedInUser, shortURLUID)
|
||||
if err != nil {
|
||||
if shorturls.ErrShortURLNotFound.Is(err) {
|
||||
return response.Err(shorturls.ErrShortURLNotFound.Errorf("shorturl not found: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, shortURL)
|
||||
}
|
||||
|
||||
type shortURLK8sHandler struct {
|
||||
namespacer request.NamespaceMapper
|
||||
gvr schema.GroupVersionResource
|
||||
clientConfigProvider grafanaapiserver.DirectRestConfigProvider
|
||||
cfg *setting.Cfg
|
||||
}
|
||||
|
||||
func newShortURLK8sHandler(hs *HTTPServer) *shortURLK8sHandler {
|
||||
gvr := schema.GroupVersionResource{
|
||||
Group: v1alpha1.ShortURLKind().Group(),
|
||||
Version: v1alpha1.ShortURLKind().Version(),
|
||||
Resource: v1alpha1.ShortURLKind().Plural(),
|
||||
}
|
||||
return &shortURLK8sHandler{
|
||||
gvr: gvr,
|
||||
namespacer: request.GetNamespaceMapper(hs.Cfg),
|
||||
clientConfigProvider: hs.clientConfigProvider,
|
||||
cfg: hs.Cfg,
|
||||
}
|
||||
}
|
||||
|
||||
func (sk8s *shortURLK8sHandler) getKubernetesShortURLsHandler(c *contextmodel.ReqContext) {
|
||||
client, ok := sk8s.getClient(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
shortURLUID := web.Params(c.Req)[":uid"]
|
||||
if !util.IsValidShortUID(shortURLUID) {
|
||||
c.JsonApiErr(http.StatusBadRequest, "Invalid short URL UID format", fmt.Errorf("invalid short URL UID: %s", shortURLUID))
|
||||
return
|
||||
}
|
||||
|
||||
c.Logger.Debug("Fetching short URL", "uid", shortURLUID)
|
||||
out, err := client.Get(c.Req.Context(), shortURLUID, v1.GetOptions{})
|
||||
if err != nil {
|
||||
sk8s.writeError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, shorturl.UnstructuredToLegacyShortURL(*out))
|
||||
}
|
||||
|
||||
func (sk8s *shortURLK8sHandler) getKubernetesRedirectFromShortURL(c *contextmodel.ReqContext) {
|
||||
client, ok := sk8s.getClient(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
shortURLUID := web.Params(c.Req)[":uid"]
|
||||
if !util.IsValidShortUID(shortURLUID) {
|
||||
c.Logger.Warn("Invalid short URL UID format", "uid", shortURLUID)
|
||||
c.Redirect(sk8s.cfg.AppURL, http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Get Object
|
||||
obj, err := client.Get(c.Req.Context(), shortURLUID, v1.GetOptions{})
|
||||
if err != nil {
|
||||
sk8s.writeError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Modify status
|
||||
status := obj.Object["status"].(map[string]interface{})
|
||||
newTimestamp := time.Now().Unix()
|
||||
status["lastSeenAt"] = newTimestamp
|
||||
|
||||
// Try status subresource first (works in Mode 5), fallback to main resource (works in Mode 0)
|
||||
out, err := client.Update(c.Req.Context(), obj, v1.UpdateOptions{}, "status")
|
||||
if err != nil {
|
||||
c.Logger.Debug("Status subresource update failed, trying main resource", "error", err)
|
||||
// Fallback to main resource update (for Mode 0)
|
||||
out, err = client.Update(c.Req.Context(), obj, v1.UpdateOptions{})
|
||||
if err != nil {
|
||||
c.Logger.Error("Both status and main resource updates failed", "error", err)
|
||||
sk8s.writeError(c, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
spec := out.Object["spec"].(map[string]any)
|
||||
path := spec["path"].(string)
|
||||
c.Logger.Debug("Redirecting short URL", "uid", shortURLUID, "path", path)
|
||||
c.Redirect(setting.ToAbsUrl(path), http.StatusFound)
|
||||
}
|
||||
|
||||
func (sk8s *shortURLK8sHandler) createKubernetesShortURLsHandler(c *contextmodel.ReqContext) {
|
||||
client, ok := sk8s.getClient(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
cmd := dtos.CreateShortURLCmd{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
c.Logger.Error("Failed to bind request data", "error", err)
|
||||
c.JsonApiErr(http.StatusBadRequest, "bad request data", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Logger.Debug("Creating short URL", "path", cmd.Path)
|
||||
obj := shorturl.LegacyCreateCommandToUnstructured(cmd)
|
||||
|
||||
uid, err := shortid.Generate()
|
||||
if err != nil {
|
||||
c.JsonApiErr(http.StatusInternalServerError, "failed to generate uid", err)
|
||||
return
|
||||
}
|
||||
obj.SetGenerateName(uid)
|
||||
|
||||
out, err := client.Create(c.Req.Context(), &obj, v1.CreateOptions{})
|
||||
if err != nil {
|
||||
c.Logger.Error("Failed to create short URL in Kubernetes", "path", cmd.Path, "error", err)
|
||||
sk8s.writeError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Logger.Info("Successfully created short URL", "path", cmd.Path, "uid", out.GetName())
|
||||
c.JSON(http.StatusOK, shorturl.UnstructuredToLegacyShortURLDTO(*out, sk8s.cfg.AppURL))
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------
|
||||
// Utility functions
|
||||
//-----------------------------------------------------------------------------------------
|
||||
|
||||
func (sk8s *shortURLK8sHandler) getClient(c *contextmodel.ReqContext) (dynamic.ResourceInterface, bool) {
|
||||
dyn, err := dynamic.NewForConfig(sk8s.clientConfigProvider.GetDirectRestConfig(c))
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "client", err)
|
||||
return nil, false
|
||||
}
|
||||
return dyn.Resource(sk8s.gvr).Namespace(sk8s.namespacer(c.OrgID)), true
|
||||
}
|
||||
|
||||
func (sk8s *shortURLK8sHandler) writeError(c *contextmodel.ReqContext, err error) {
|
||||
//nolint:errorlint
|
||||
statusError, ok := err.(*errors.StatusError)
|
||||
if ok {
|
||||
c.JsonApiErr(int(statusError.Status().Code), statusError.Status().Message, err)
|
||||
return
|
||||
}
|
||||
errhttp.Write(c.Req.Context(), err, c.Resp)
|
||||
}
|
||||
|
||||
@@ -31,9 +31,12 @@ func TestShortURLAPIEndpoint(t *testing.T) {
|
||||
Path: cmd.Path,
|
||||
}
|
||||
service := &fakeShortURLService{
|
||||
createShortURLFunc: func(ctx context.Context, user *user.SignedInUser, path string) (*shorturls.ShortUrl, error) {
|
||||
createShortURLFunc: func(ctx context.Context, user *user.SignedInUser, cmd *dtos.CreateShortURLCmd) (*shorturls.ShortUrl, error) {
|
||||
return createResp, nil
|
||||
},
|
||||
createConvertShortURLToDTO: func(shortURL *shorturls.ShortUrl, appURL string) *dtos.ShortURL {
|
||||
return &dtos.ShortURL{UID: createResp.Uid, URL: "http://localhost:3000/goto/N1u6L4eGz?orgId=1"}
|
||||
},
|
||||
}
|
||||
|
||||
createShortURLScenario(t, "When calling POST on", "/api/short-urls", "/api/short-urls", cmd, service,
|
||||
@@ -44,7 +47,7 @@ func TestShortURLAPIEndpoint(t *testing.T) {
|
||||
err := json.NewDecoder(sc.resp.Body).Decode(&shortUrl)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 200, sc.resp.Code)
|
||||
require.Equal(t, fmt.Sprintf("/goto/%s?orgId=%d", createResp.Uid, createResp.OrgId), shortUrl.URL)
|
||||
require.Equal(t, fmt.Sprintf("http://localhost:3000/goto/%s?orgId=%d", createResp.Uid, createResp.OrgId), shortUrl.URL)
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -78,16 +81,17 @@ func createShortURLScenario(t *testing.T, desc string, url string, routePattern
|
||||
}
|
||||
|
||||
type fakeShortURLService struct {
|
||||
createShortURLFunc func(ctx context.Context, user *user.SignedInUser, path string) (*shorturls.ShortUrl, error)
|
||||
createShortURLFunc func(ctx context.Context, user *user.SignedInUser, cmd *dtos.CreateShortURLCmd) (*shorturls.ShortUrl, error)
|
||||
createConvertShortURLToDTO func(shortURL *shorturls.ShortUrl, appURL string) *dtos.ShortURL
|
||||
}
|
||||
|
||||
func (s *fakeShortURLService) GetShortURLByUID(ctx context.Context, user *user.SignedInUser, uid string) (*shorturls.ShortUrl, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *fakeShortURLService) CreateShortURL(ctx context.Context, user *user.SignedInUser, path string) (*shorturls.ShortUrl, error) {
|
||||
func (s *fakeShortURLService) CreateShortURL(ctx context.Context, user *user.SignedInUser, cmd *dtos.CreateShortURLCmd) (*shorturls.ShortUrl, error) {
|
||||
if s.createShortURLFunc != nil {
|
||||
return s.createShortURLFunc(ctx, user, path)
|
||||
return s.createShortURLFunc(ctx, user, cmd)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
@@ -100,3 +104,10 @@ func (s *fakeShortURLService) UpdateLastSeenAt(ctx context.Context, shortURL *sh
|
||||
func (s *fakeShortURLService) DeleteStaleShortURLs(ctx context.Context, cmd *shorturls.DeleteShortUrlCommand) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *fakeShortURLService) ConvertShortURLToDTO(shortURL *shorturls.ShortUrl, appURL string) *dtos.ShortURL {
|
||||
if s.createConvertShortURLToDTO != nil {
|
||||
return s.createConvertShortURLToDTO(shortURL, appURL)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,12 +2,15 @@ package shorturl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
|
||||
shorturl "github.com/grafana/grafana/apps/shorturl/pkg/apis/shorturl/v1alpha1"
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||
"github.com/grafana/grafana/pkg/services/shorturls"
|
||||
)
|
||||
@@ -32,3 +35,37 @@ func convertToK8sResource(v *shorturls.ShortUrl, namespacer request.NamespaceMap
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func LegacyCreateCommandToUnstructured(cmd dtos.CreateShortURLCmd) unstructured.Unstructured {
|
||||
obj := unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"metadata": map[string]interface{}{
|
||||
"name": cmd.UID,
|
||||
},
|
||||
"spec": map[string]interface{}{
|
||||
"path": cmd.Path,
|
||||
},
|
||||
},
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
func UnstructuredToLegacyShortURLDTO(item unstructured.Unstructured, appURL string) *dtos.ShortURL {
|
||||
url := fmt.Sprintf("%s/goto/%s?orgId=%s", strings.TrimSuffix(appURL, "/"), item.GetName(), item.GetNamespace())
|
||||
|
||||
return &dtos.ShortURL{
|
||||
UID: item.GetName(),
|
||||
URL: url,
|
||||
}
|
||||
}
|
||||
|
||||
func UnstructuredToLegacyShortURL(item unstructured.Unstructured) *shorturls.ShortUrl {
|
||||
spec := item.Object["spec"].(map[string]interface{})
|
||||
status := item.Object["status"].(map[string]interface{})
|
||||
|
||||
return &shorturls.ShortUrl{
|
||||
Uid: item.GetName(),
|
||||
Path: spec["path"].(string),
|
||||
LastSeenAt: status["lastSeenAt"].(int64),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
|
||||
shorturl "github.com/grafana/grafana/apps/shorturl/pkg/apis/shorturl/v1alpha1"
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||
"github.com/grafana/grafana/pkg/services/authn"
|
||||
@@ -112,11 +113,20 @@ func (s *legacyStorage) Create(ctx context.Context,
|
||||
return nil, fmt.Errorf("unsupported identity type")
|
||||
}
|
||||
|
||||
if createValidation != nil {
|
||||
if err := createValidation(ctx, obj.DeepCopyObject()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
p, ok := obj.(*shorturl.ShortURL)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected shorturl?")
|
||||
}
|
||||
out, err := s.service.CreateShortURL(ctx, signedInUser, p.Spec.Path)
|
||||
cmd := &dtos.CreateShortURLCmd{
|
||||
Path: p.Spec.Path,
|
||||
UID: p.Name,
|
||||
}
|
||||
out, err := s.service.CreateShortURL(ctx, signedInUser, cmd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -131,12 +141,58 @@ func (s *legacyStorage) Update(ctx context.Context,
|
||||
forceAllowCreate bool,
|
||||
options *metav1.UpdateOptions,
|
||||
) (runtime.Object, bool, error) {
|
||||
return nil, false, fmt.Errorf("Update for shorturl not implemented")
|
||||
// For other updates, use the original logic
|
||||
requester, err := identity.GetRequester(ctx)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
// Convert identity.Requester to *user.SignedInUser
|
||||
var signedInUser *user.SignedInUser
|
||||
if authnIdentity, ok := requester.(*authn.Identity); ok {
|
||||
signedInUser = authnIdentity.SignedInUser()
|
||||
} else if userIdentity, ok := requester.(*user.SignedInUser); ok {
|
||||
signedInUser = userIdentity
|
||||
} else {
|
||||
return nil, false, fmt.Errorf("unsupported identity type")
|
||||
}
|
||||
|
||||
shortURL, err := s.service.GetShortURLByUID(ctx, signedInUser, name)
|
||||
if err != nil || shortURL == nil {
|
||||
if errors.Is(err, shorturls.ErrShortURLNotFound) || err == nil {
|
||||
err = k8serrors.NewNotFound(schema.GroupResource{
|
||||
Group: shorturl.ShortURLKind().Group(),
|
||||
Resource: shorturl.ShortURLKind().Plural(),
|
||||
}, name)
|
||||
}
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
err = s.service.UpdateLastSeenAt(ctx, shortURL)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
// Fetch the updated short URL to return
|
||||
updatedLegacyShortURL, err := s.service.GetShortURLByUID(ctx, signedInUser, name)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
return convertToK8sResource(updatedLegacyShortURL, s.namespacer), true, nil
|
||||
}
|
||||
|
||||
// GracefulDeleter
|
||||
func (s *legacyStorage) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
|
||||
return nil, false, fmt.Errorf("Delete for shorturl not implemented")
|
||||
v, err := s.Get(ctx, name, &metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return v, false, err // includes the not-found error
|
||||
}
|
||||
p, ok := v.(*shorturl.ShortURL)
|
||||
if !ok {
|
||||
return v, false, fmt.Errorf("expected a shorturl response from Get")
|
||||
}
|
||||
err = s.service.DeleteStaleShortURLs(ctx, &shorturls.DeleteShortUrlCommand{Uid: name})
|
||||
return p, true, err // true is instant delete
|
||||
}
|
||||
|
||||
// CollectionDeleter
|
||||
|
||||
@@ -12,19 +12,21 @@ var (
|
||||
ErrShortURLAbsolutePath = errutil.ValidationFailed("shorturl.absolute-path", errutil.WithPublicMessage("Path should be relative"))
|
||||
ErrShortURLInvalidPath = errutil.ValidationFailed("shorturl.invalid-path", errutil.WithPublicMessage("Invalid short URL path"))
|
||||
ErrShortURLInternal = errutil.Internal("shorturl.internal")
|
||||
ErrShortURLConflict = errutil.Conflict("shorturl.conflict")
|
||||
)
|
||||
|
||||
type ShortUrl struct {
|
||||
Id int64
|
||||
OrgId int64
|
||||
Uid string
|
||||
Path string
|
||||
CreatedBy int64
|
||||
CreatedAt int64
|
||||
LastSeenAt int64
|
||||
Id int64 `json:"-"`
|
||||
OrgId int64 `json:"-"`
|
||||
Uid string `json:"uid"`
|
||||
Path string `json:"path"`
|
||||
CreatedBy int64 `json:"-"`
|
||||
CreatedAt int64 `json:"-"`
|
||||
LastSeenAt int64 `json:"lastSeenAt"`
|
||||
}
|
||||
|
||||
type DeleteShortUrlCommand struct {
|
||||
Uid string
|
||||
OlderThan time.Time
|
||||
|
||||
NumDeleted int64
|
||||
|
||||
@@ -3,12 +3,14 @@ package shorturls
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
GetShortURLByUID(ctx context.Context, user *user.SignedInUser, uid string) (*ShortUrl, error)
|
||||
CreateShortURL(ctx context.Context, user *user.SignedInUser, path string) (*ShortUrl, error)
|
||||
CreateShortURL(ctx context.Context, user *user.SignedInUser, cmd *dtos.CreateShortURLCmd) (*ShortUrl, error)
|
||||
UpdateLastSeenAt(ctx context.Context, shortURL *ShortUrl) error
|
||||
DeleteStaleShortURLs(ctx context.Context, cmd *DeleteShortUrlCommand) error
|
||||
ConvertShortURLToDTO(shortURL *ShortUrl, appURL string) *dtos.ShortURL
|
||||
}
|
||||
|
||||
@@ -2,13 +2,16 @@ package shorturlimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/services/shorturls"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/teris-io/shortid"
|
||||
)
|
||||
|
||||
@@ -34,8 +37,8 @@ func (s ShortURLService) UpdateLastSeenAt(ctx context.Context, shortURL *shortur
|
||||
return s.SQLStore.Update(ctx, shortURL)
|
||||
}
|
||||
|
||||
func (s ShortURLService) CreateShortURL(ctx context.Context, user *user.SignedInUser, relPath string) (*shorturls.ShortUrl, error) {
|
||||
relPath = strings.TrimSpace(relPath)
|
||||
func (s ShortURLService) CreateShortURL(ctx context.Context, user *user.SignedInUser, cmd *dtos.CreateShortURLCmd) (*shorturls.ShortUrl, error) {
|
||||
relPath := strings.TrimSpace(cmd.Path)
|
||||
|
||||
if path.IsAbs(relPath) {
|
||||
return nil, shorturls.ErrShortURLAbsolutePath.Errorf("expected relative path: %s", relPath)
|
||||
@@ -44,9 +47,30 @@ func (s ShortURLService) CreateShortURL(ctx context.Context, user *user.SignedIn
|
||||
return nil, shorturls.ErrShortURLInvalidPath.Errorf("path cannot contain '../': %s", relPath)
|
||||
}
|
||||
|
||||
uid, err := shortid.Generate()
|
||||
if err != nil {
|
||||
return nil, shorturls.ErrShortURLInternal.Errorf("failed to generate uid: %w", err)
|
||||
uid := cmd.UID
|
||||
if uid == "" {
|
||||
var err error
|
||||
uid, err = shortid.Generate()
|
||||
if err != nil {
|
||||
return nil, shorturls.ErrShortURLInternal.Errorf("failed to generate uid: %w", err)
|
||||
}
|
||||
} else {
|
||||
// Ensure the UID is valid
|
||||
if !util.IsValidShortUID(uid) {
|
||||
return nil, shorturls.ErrShortURLBadRequest.Errorf("invalid UID: %s", uid)
|
||||
}
|
||||
|
||||
// Check if the UID already exists
|
||||
existingShortURL, err := s.SQLStore.Get(ctx, user, uid)
|
||||
if err != nil {
|
||||
if !shorturls.ErrShortURLNotFound.Is(err) {
|
||||
return nil, shorturls.ErrShortURLInternal.Errorf("failed to check existing short URL: %w", err)
|
||||
}
|
||||
}
|
||||
if existingShortURL != nil {
|
||||
// If the UID already exists, we return an error
|
||||
return nil, shorturls.ErrShortURLConflict.Errorf("short URL with UID '%s' already exists", uid)
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now().Unix()
|
||||
@@ -68,3 +92,12 @@ func (s ShortURLService) CreateShortURL(ctx context.Context, user *user.SignedIn
|
||||
func (s ShortURLService) DeleteStaleShortURLs(ctx context.Context, cmd *shorturls.DeleteShortUrlCommand) error {
|
||||
return s.SQLStore.Delete(ctx, cmd)
|
||||
}
|
||||
|
||||
func (s ShortURLService) ConvertShortURLToDTO(shortURL *shorturls.ShortUrl, appURL string) *dtos.ShortURL {
|
||||
url := fmt.Sprintf("%s/goto/%s?orgId=%d", strings.TrimSuffix(appURL, "/"), shortURL.Uid, shortURL.OrgId)
|
||||
|
||||
return &dtos.ShortURL{
|
||||
UID: shortURL.Uid,
|
||||
URL: url,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/services/shorturls"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
@@ -25,11 +26,13 @@ func TestIntegrationShortURLService(t *testing.T) {
|
||||
store := db.InitTestDB(t)
|
||||
|
||||
t.Run("User can create and read short URLs", func(t *testing.T) {
|
||||
const refPath = "mock/path?test=true"
|
||||
cmd := &dtos.CreateShortURLCmd{
|
||||
Path: "mock/path?test=true",
|
||||
}
|
||||
|
||||
service := ShortURLService{SQLStore: &sqlStore{db: store}}
|
||||
|
||||
newShortURL, err := service.CreateShortURL(context.Background(), user, refPath)
|
||||
newShortURL, err := service.CreateShortURL(context.Background(), user, cmd)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, newShortURL)
|
||||
require.NotEmpty(t, newShortURL.Uid)
|
||||
@@ -37,7 +40,7 @@ func TestIntegrationShortURLService(t *testing.T) {
|
||||
existingShortURL, err := service.GetShortURLByUID(context.Background(), user, newShortURL.Uid)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, existingShortURL)
|
||||
require.Equal(t, refPath, existingShortURL.Path)
|
||||
require.Equal(t, cmd.Path, existingShortURL.Path)
|
||||
|
||||
t.Run("and update last seen at", func(t *testing.T) {
|
||||
origGetTime := getTime
|
||||
@@ -59,7 +62,7 @@ func TestIntegrationShortURLService(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("and stale short urls can be deleted", func(t *testing.T) {
|
||||
staleShortURL, err := service.CreateShortURL(context.Background(), user, refPath)
|
||||
staleShortURL, err := service.CreateShortURL(context.Background(), user, cmd)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, staleShortURL)
|
||||
require.NotEmpty(t, staleShortURL.Uid)
|
||||
@@ -100,18 +103,25 @@ func TestIntegrationShortURLService(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
absolutePath := "/path?test=true"
|
||||
newShortURL, err := service.CreateShortURL(ctx, user, absolutePath)
|
||||
cmd := &dtos.CreateShortURLCmd{
|
||||
Path: "/path?test=true",
|
||||
}
|
||||
|
||||
newShortURL, err := service.CreateShortURL(ctx, user, cmd)
|
||||
require.ErrorIs(t, err, shorturls.ErrShortURLAbsolutePath)
|
||||
require.Nil(t, newShortURL)
|
||||
|
||||
relativePath := "path/../test?test=true"
|
||||
newShortURL, err = service.CreateShortURL(ctx, user, relativePath)
|
||||
cmd2 := &dtos.CreateShortURLCmd{
|
||||
Path: "path/../test?test=true",
|
||||
}
|
||||
newShortURL, err = service.CreateShortURL(ctx, user, cmd2)
|
||||
require.ErrorIs(t, err, shorturls.ErrShortURLInvalidPath)
|
||||
require.Nil(t, newShortURL)
|
||||
|
||||
relativePath = "../path/test?test=true"
|
||||
newShortURL, err = service.CreateShortURL(ctx, user, relativePath)
|
||||
cmd3 := &dtos.CreateShortURLCmd{
|
||||
Path: "../path/test?test=true",
|
||||
}
|
||||
newShortURL, err = service.CreateShortURL(ctx, user, cmd3)
|
||||
require.ErrorIs(t, err, shorturls.ErrShortURLInvalidPath)
|
||||
require.Nil(t, newShortURL)
|
||||
})
|
||||
@@ -121,14 +131,15 @@ func TestIntegrationShortURLService(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
const refPath = "mock/path?test=true"
|
||||
|
||||
newShortURL1, err := service.CreateShortURL(ctx, user, refPath)
|
||||
cmd := &dtos.CreateShortURLCmd{
|
||||
Path: "mock/path?test=true",
|
||||
}
|
||||
newShortURL1, err := service.CreateShortURL(ctx, user, cmd)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, newShortURL1)
|
||||
require.NotEmpty(t, newShortURL1.Uid)
|
||||
|
||||
newShortURL2, err := service.CreateShortURL(ctx, user, refPath)
|
||||
newShortURL2, err := service.CreateShortURL(ctx, user, cmd)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, newShortURL2)
|
||||
require.NotEmpty(t, newShortURL2.Uid)
|
||||
@@ -136,4 +147,38 @@ func TestIntegrationShortURLService(t *testing.T) {
|
||||
require.NotEqual(t, newShortURL1.Uid, newShortURL2.Uid)
|
||||
require.Equal(t, newShortURL1.Path, newShortURL2.Path)
|
||||
})
|
||||
|
||||
t.Run("Create URL providing the UID", func(t *testing.T) {
|
||||
service := ShortURLService{SQLStore: &sqlStore{db: store}}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
cmd := &dtos.CreateShortURLCmd{
|
||||
Path: "mock/path?test=true",
|
||||
UID: "custom-uid",
|
||||
}
|
||||
newShortURL1, err := service.CreateShortURL(ctx, user, cmd)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, newShortURL1)
|
||||
require.Equal(t, cmd.UID, newShortURL1.Uid)
|
||||
})
|
||||
|
||||
t.Run("Create URL providing an existing UID should fail", func(t *testing.T) {
|
||||
service := ShortURLService{SQLStore: &sqlStore{db: store}}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
cmd := &dtos.CreateShortURLCmd{
|
||||
Path: "mock/path?test=true",
|
||||
UID: "custom-uid-2",
|
||||
}
|
||||
newShortURL1, err := service.CreateShortURL(ctx, user, cmd)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, newShortURL1)
|
||||
require.Equal(t, cmd.UID, newShortURL1.Uid)
|
||||
|
||||
newShortURL2, err := service.CreateShortURL(ctx, user, cmd)
|
||||
require.ErrorIs(t, err, shorturls.ErrShortURLConflict)
|
||||
require.Nil(t, newShortURL2)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -58,6 +58,21 @@ func (s sqlStore) Insert(ctx context.Context, shortURL *shorturls.ShortUrl) erro
|
||||
}
|
||||
|
||||
func (s sqlStore) Delete(ctx context.Context, cmd *shorturls.DeleteShortUrlCommand) error {
|
||||
// If a UID is provided, delete that specific short URL
|
||||
if cmd.Uid != "" {
|
||||
return s.db.WithTransactionalDbSession(ctx, func(session *db.Session) error {
|
||||
var rawSql = "DELETE FROM short_url WHERE uid = ?"
|
||||
|
||||
if result, err := session.Exec(rawSql, cmd.Uid); err != nil {
|
||||
return err
|
||||
} else if cmd.NumDeleted, err = result.RowsAffected(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Otherwise, delete all stale short URLs older than the specified time
|
||||
return s.db.WithTransactionalDbSession(ctx, func(session *db.Session) error {
|
||||
var rawSql = "DELETE FROM short_url WHERE created_at <= ? AND (last_seen_at IS NULL OR last_seen_at = 0)"
|
||||
|
||||
|
||||
@@ -0,0 +1,539 @@
|
||||
package shorturl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/shorturls"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
grafanarest "github.com/grafana/grafana/pkg/apiserver/rest"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/options"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/tests/apis"
|
||||
"github.com/grafana/grafana/pkg/tests/testinfra"
|
||||
"github.com/grafana/grafana/pkg/tests/testsuite"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
testsuite.Run(m)
|
||||
}
|
||||
|
||||
var gvr = schema.GroupVersionResource{
|
||||
Group: "shorturl.grafana.app",
|
||||
Version: "v1alpha1",
|
||||
Resource: "shorturls",
|
||||
}
|
||||
|
||||
var RESOURCEGROUP = gvr.GroupResource().String()
|
||||
|
||||
func TestIntegrationShortURL(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
|
||||
t.Run("default setup with k8s flag turned off (legacy APIs)", func(t *testing.T) {
|
||||
helper := apis.NewK8sTestHelper(t, testinfra.GrafanaOpts{
|
||||
AppModeProduction: true, // do not start extra port 6443
|
||||
DisableAnonymous: true,
|
||||
EnableFeatureToggles: []string{}, // legacy APIs only
|
||||
})
|
||||
// In this setup, K8s APIs are not available - legacy APIs only
|
||||
doLegacyOnlyTests(t, helper)
|
||||
|
||||
// When no feature toggles are enabled, shortURL K8s APIs should not be available
|
||||
disco := helper.NewDiscoveryClient()
|
||||
groups, err := disco.ServerGroups()
|
||||
require.NoError(t, err)
|
||||
|
||||
hasShortURLGroup := false
|
||||
for _, group := range groups.Groups {
|
||||
if group.Name == "shorturl.grafana.app" {
|
||||
hasShortURLGroup = true
|
||||
break
|
||||
}
|
||||
}
|
||||
require.False(t, hasShortURLGroup, "shortURL K8s APIs should not be available when kubernetesShortURLs feature toggle is disabled")
|
||||
})
|
||||
|
||||
t.Run("with dual write (unified storage, mode 0)", func(t *testing.T) {
|
||||
helper := apis.NewK8sTestHelper(t, testinfra.GrafanaOpts{
|
||||
AppModeProduction: false, // required for unified storage
|
||||
DisableAnonymous: true,
|
||||
APIServerStorageType: options.StorageTypeUnified,
|
||||
EnableFeatureToggles: []string{"kubernetesShortURLs"},
|
||||
UnifiedStorageConfig: map[string]setting.UnifiedStorageConfig{
|
||||
RESOURCEGROUP: {
|
||||
DualWriterMode: grafanarest.Mode0,
|
||||
},
|
||||
},
|
||||
})
|
||||
doLegacyOnlyTests(t, helper)
|
||||
})
|
||||
|
||||
t.Run("with dual write (unified storage, mode 1)", func(t *testing.T) {
|
||||
mode := grafanarest.Mode1
|
||||
helper := apis.NewK8sTestHelper(t, testinfra.GrafanaOpts{
|
||||
AppModeProduction: false,
|
||||
DisableAnonymous: true,
|
||||
APIServerStorageType: options.StorageTypeUnified,
|
||||
EnableFeatureToggles: []string{"kubernetesShortURLs"},
|
||||
UnifiedStorageConfig: map[string]setting.UnifiedStorageConfig{
|
||||
RESOURCEGROUP: {
|
||||
DualWriterMode: mode,
|
||||
},
|
||||
},
|
||||
})
|
||||
doDualWriteTests(t, helper, mode)
|
||||
})
|
||||
|
||||
t.Run("with dual write (unified storage, mode 2)", func(t *testing.T) {
|
||||
mode := grafanarest.Mode2
|
||||
helper := apis.NewK8sTestHelper(t, testinfra.GrafanaOpts{
|
||||
AppModeProduction: false,
|
||||
DisableAnonymous: true,
|
||||
APIServerStorageType: options.StorageTypeUnified,
|
||||
EnableFeatureToggles: []string{"kubernetesShortURLs"},
|
||||
UnifiedStorageConfig: map[string]setting.UnifiedStorageConfig{
|
||||
RESOURCEGROUP: {
|
||||
DualWriterMode: mode,
|
||||
},
|
||||
},
|
||||
})
|
||||
doDualWriteTests(t, helper, mode)
|
||||
})
|
||||
|
||||
t.Run("with dual write (unified storage, mode 3)", func(t *testing.T) {
|
||||
mode := grafanarest.Mode3
|
||||
helper := apis.NewK8sTestHelper(t, testinfra.GrafanaOpts{
|
||||
AppModeProduction: false,
|
||||
DisableAnonymous: true,
|
||||
APIServerStorageType: options.StorageTypeUnified,
|
||||
EnableFeatureToggles: []string{"kubernetesShortURLs"},
|
||||
UnifiedStorageConfig: map[string]setting.UnifiedStorageConfig{
|
||||
RESOURCEGROUP: {
|
||||
DualWriterMode: mode,
|
||||
},
|
||||
},
|
||||
})
|
||||
doDualWriteTests(t, helper, mode)
|
||||
})
|
||||
|
||||
t.Run("with dual write (unified storage, mode 5)", func(t *testing.T) {
|
||||
helper := apis.NewK8sTestHelper(t, testinfra.GrafanaOpts{
|
||||
AppModeProduction: false,
|
||||
DisableAnonymous: true,
|
||||
APIServerStorageType: options.StorageTypeUnified,
|
||||
EnableFeatureToggles: []string{"kubernetesShortURLs"},
|
||||
UnifiedStorageConfig: map[string]setting.UnifiedStorageConfig{
|
||||
RESOURCEGROUP: {
|
||||
DualWriterMode: grafanarest.Mode5,
|
||||
},
|
||||
},
|
||||
})
|
||||
doUnifiedOnlyTests(t, helper)
|
||||
})
|
||||
}
|
||||
|
||||
// doLegacyOnlyTests tests functionality for Mode 0 (legacy only)
|
||||
// Only legacy API should be used, no K8s API interaction
|
||||
func doLegacyOnlyTests(t *testing.T, helper *apis.K8sTestHelper) {
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Editor,
|
||||
GVR: gvr,
|
||||
})
|
||||
|
||||
t.Run("Legacy API CRUD", func(t *testing.T) {
|
||||
// Create via legacy API
|
||||
legacyPayload := `{
|
||||
"path": "d/xCmMwXdVz/legacy-only-test"
|
||||
}`
|
||||
legacyCreate := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/short-urls",
|
||||
Body: []byte(legacyPayload),
|
||||
}, &dtos.ShortURL{})
|
||||
require.NotNil(t, legacyCreate.Result)
|
||||
uid := legacyCreate.Result.UID
|
||||
require.NotEmpty(t, uid)
|
||||
|
||||
// Read via legacy API
|
||||
legacyGet := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/short-urls/" + uid,
|
||||
}, &shorturls.ShortUrl{})
|
||||
require.NotNil(t, legacyGet.Result)
|
||||
assert.Equal(t, uid, legacyGet.Result.Uid)
|
||||
assert.Equal(t, "d/xCmMwXdVz/legacy-only-test", legacyGet.Result.Path)
|
||||
})
|
||||
|
||||
t.Run("Legacy API redirect functionality", func(t *testing.T) {
|
||||
// Create via legacy API
|
||||
legacyPayload := `{
|
||||
"path": "d/test/legacy-redirect"
|
||||
}`
|
||||
legacyCreate := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/short-urls",
|
||||
Body: []byte(legacyPayload),
|
||||
}, &dtos.ShortURL{})
|
||||
require.NotNil(t, legacyCreate.Result)
|
||||
uid := legacyCreate.Result.UID
|
||||
|
||||
// Test redirect functionality
|
||||
redirectResponse := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodGet,
|
||||
Path: "/goto/" + uid + "?orgId=default",
|
||||
}, (*interface{})(nil))
|
||||
assert.Equal(t, 302, redirectResponse.Response.StatusCode)
|
||||
})
|
||||
}
|
||||
|
||||
// doDualWriteTests tests functionality for Modes 1-3 (dual write modes)
|
||||
// Both APIs available with cross-API visibility
|
||||
func doDualWriteTests(t *testing.T, helper *apis.K8sTestHelper, mode grafanarest.DualWriterMode) {
|
||||
// Check if shortURL K8s APIs are available
|
||||
hasShortURLAPI := checkShortURLAPIAvailable(t, helper)
|
||||
if !hasShortURLAPI {
|
||||
t.Log("ShortURL Kubernetes APIs not available - skipping K8s API tests")
|
||||
return
|
||||
}
|
||||
|
||||
t.Run("Legacy API -> K8s API visibility", func(t *testing.T) {
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Editor,
|
||||
GVR: gvr,
|
||||
})
|
||||
|
||||
// Create via legacy API
|
||||
legacyPayload := `{
|
||||
"path": "d/xCmMwXdVz/dual-write-test"
|
||||
}`
|
||||
legacyCreate := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/short-urls",
|
||||
Body: []byte(legacyPayload),
|
||||
}, &dtos.ShortURL{})
|
||||
require.NotNil(t, legacyCreate.Result)
|
||||
uid := legacyCreate.Result.UID
|
||||
require.NotEmpty(t, uid)
|
||||
|
||||
// Should be visible via K8s API
|
||||
found, err := client.Resource.Get(context.Background(), uid, metav1.GetOptions{})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, uid, found.GetName())
|
||||
|
||||
// Verify cross-API consistency
|
||||
getFromBothAPIs(t, helper, client, uid)
|
||||
|
||||
// Clean up
|
||||
err = client.Resource.Delete(context.Background(), uid, metav1.DeleteOptions{})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("K8s API -> Legacy API visibility", func(t *testing.T) {
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Editor,
|
||||
GVR: gvr,
|
||||
})
|
||||
|
||||
// Create via K8s API
|
||||
obj := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodPost,
|
||||
Path: "/apis/shorturl.grafana.app/v1alpha1/namespaces/default/shorturls",
|
||||
Body: []byte(`{ "metadata": { "generateName": "test-" }, "spec": { "path": "d/xCmMwXdVz/k8s-dual-write" } }`),
|
||||
}, &unstructured.Unstructured{})
|
||||
require.NotNil(t, obj.Result)
|
||||
|
||||
uid := obj.Result.GetName()
|
||||
assert.NotEmpty(t, uid)
|
||||
|
||||
// Should be visible via legacy API
|
||||
legacyShortURL := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/short-urls/" + uid,
|
||||
}, &shorturls.ShortUrl{}).Result
|
||||
require.NotNil(t, legacyShortURL)
|
||||
assert.Equal(t, uid, legacyShortURL.Uid)
|
||||
|
||||
// Verify cross-API consistency
|
||||
getFromBothAPIs(t, helper, client, uid)
|
||||
|
||||
// Clean up
|
||||
err := client.Resource.Delete(context.Background(), uid, metav1.DeleteOptions{})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Redirect functionality", func(t *testing.T) {
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Editor,
|
||||
GVR: gvr,
|
||||
})
|
||||
|
||||
// Create via K8s API
|
||||
obj := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodPost,
|
||||
Path: "/apis/shorturl.grafana.app/v1alpha1/namespaces/default/shorturls",
|
||||
Body: []byte(`{ "metadata": { "generateName": "redirect-" }, "spec": { "path": "d/test/redirect" } }`),
|
||||
}, &unstructured.Unstructured{})
|
||||
require.NotNil(t, obj.Result)
|
||||
|
||||
uid := obj.Result.GetName()
|
||||
|
||||
// Test redirect functionality and lastSeenAt update
|
||||
redirectResponse := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodGet,
|
||||
Path: "/goto/" + uid + "?orgId=default",
|
||||
}, (*interface{})(nil))
|
||||
assert.Equal(t, 302, redirectResponse.Response.StatusCode)
|
||||
|
||||
// Verify lastSeenAt was updated (should be > 0 now)
|
||||
found, err := client.Resource.Get(context.Background(), uid, metav1.GetOptions{})
|
||||
require.NoError(t, err)
|
||||
status, exists := found.Object["status"].(map[string]interface{})
|
||||
assert.True(t, exists)
|
||||
lastSeenAt, exists := status["lastSeenAt"].(int64)
|
||||
assert.True(t, exists)
|
||||
|
||||
assert.Greater(t, lastSeenAt, int64(0))
|
||||
|
||||
// Clean up
|
||||
err = client.Resource.Delete(context.Background(), uid, metav1.DeleteOptions{})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
// doUnifiedOnlyTests tests functionality for Modes 4-5 (unified only)
|
||||
// Only K8s API, no legacy API interaction
|
||||
func doUnifiedOnlyTests(t *testing.T, helper *apis.K8sTestHelper) {
|
||||
// Check if shortURL K8s APIs are available
|
||||
hasShortURLAPI := checkShortURLAPIAvailable(t, helper)
|
||||
if !hasShortURLAPI {
|
||||
t.Log("ShortURL Kubernetes APIs not available - skipping K8s API tests")
|
||||
return
|
||||
}
|
||||
|
||||
t.Run("K8s API CRUD (unified storage only)", func(t *testing.T) {
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Editor,
|
||||
GVR: gvr,
|
||||
})
|
||||
|
||||
// Create via K8s API
|
||||
obj := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodPost,
|
||||
Path: "/apis/shorturl.grafana.app/v1alpha1/namespaces/default/shorturls",
|
||||
Body: []byte(`{ "metadata": { "generateName": "unified-" }, "spec": { "path": "d/xCmMwXdVz/unified-only" } }`),
|
||||
}, &unstructured.Unstructured{})
|
||||
require.NotNil(t, obj.Result)
|
||||
|
||||
uid := obj.Result.GetName()
|
||||
assert.NotEmpty(t, uid)
|
||||
|
||||
// Read via K8s API
|
||||
found, err := client.Resource.Get(context.Background(), uid, metav1.GetOptions{})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, uid, found.GetName())
|
||||
|
||||
// Should NOT be visible via legacy API in unified-only mode
|
||||
legacyResponse := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/short-urls/" + uid,
|
||||
}, (*shorturls.ShortUrl)(nil))
|
||||
// In unified-only mode, legacy API should not see the resource
|
||||
assert.Nil(t, legacyResponse.Result)
|
||||
|
||||
// Clean up
|
||||
err = client.Resource.Delete(context.Background(), uid, metav1.DeleteOptions{})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("K8s API validation - invalid paths", func(t *testing.T) {
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Editor,
|
||||
GVR: gvr,
|
||||
})
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
path string
|
||||
expectedError string
|
||||
}{
|
||||
{
|
||||
name: "absolute path should be rejected",
|
||||
path: "/dashboard/absolute-path",
|
||||
expectedError: "path should be relative",
|
||||
},
|
||||
{
|
||||
name: "path with directory traversal should be rejected",
|
||||
path: "d/../../../etc/passwd",
|
||||
expectedError: "invalid short URL path",
|
||||
},
|
||||
{
|
||||
name: "path with multiple directory traversals should be rejected",
|
||||
path: "d/some/../path/../../../secret",
|
||||
expectedError: "invalid short URL path",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Attempt to create ShortURL with invalid path
|
||||
invalidBody := fmt.Sprintf(`{ "metadata": { "generateName": "invalid-" }, "spec": { "path": "%s" } }`, tc.path)
|
||||
response := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodPost,
|
||||
Path: "/apis/shorturl.grafana.app/v1alpha1/namespaces/default/shorturls",
|
||||
Body: []byte(invalidBody),
|
||||
}, (*unstructured.Unstructured)(nil))
|
||||
|
||||
// Should get a validation error, it should be 400 Bad Request but the validation hook returns 403 Forbidden
|
||||
assert.Equal(t, http.StatusForbidden, response.Response.StatusCode,
|
||||
"Expected 403 for invalid path: %s", tc.path)
|
||||
|
||||
// Check that the error message contains expected validation error
|
||||
assert.Contains(t, string(response.Body), tc.expectedError,
|
||||
"Response should contain validation error message")
|
||||
|
||||
// Should not have created a resource
|
||||
assert.Nil(t, response.Result, "No resource should be created for invalid path")
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("K8s API validation - valid edge cases", func(t *testing.T) {
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Editor,
|
||||
GVR: gvr,
|
||||
})
|
||||
|
||||
validPaths := []string{
|
||||
"d/dashboard/valid-path",
|
||||
"dashboard/some-id",
|
||||
"explore?from=123&to=456",
|
||||
"d/abc123/dashboard-with-params?var-test=value",
|
||||
}
|
||||
|
||||
for _, validPath := range validPaths {
|
||||
t.Run(fmt.Sprintf("valid path: %s", validPath), func(t *testing.T) {
|
||||
validBody := fmt.Sprintf(`{ "metadata": { "generateName": "valid-" }, "spec": { "path": "%s" } }`, validPath)
|
||||
response := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodPost,
|
||||
Path: "/apis/shorturl.grafana.app/v1alpha1/namespaces/default/shorturls",
|
||||
Body: []byte(validBody),
|
||||
}, &unstructured.Unstructured{})
|
||||
|
||||
// Should succeed
|
||||
assert.Equal(t, http.StatusCreated, response.Response.StatusCode,
|
||||
"Expected 201 Created for valid path: %s", validPath)
|
||||
assert.NotNil(t, response.Result, "Resource should be created for valid path")
|
||||
|
||||
if response.Result != nil {
|
||||
uid := response.Result.GetName()
|
||||
|
||||
// Clean up
|
||||
err := client.Resource.Delete(context.Background(), uid, metav1.DeleteOptions{})
|
||||
assert.NoError(t, err, "Cleanup should succeed")
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Redirect functionality (unified only)", func(t *testing.T) {
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Editor,
|
||||
GVR: gvr,
|
||||
})
|
||||
|
||||
// Create via K8s API
|
||||
obj := apis.DoRequest[unstructured.Unstructured](helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodPost,
|
||||
Path: "/apis/shorturl.grafana.app/v1alpha1/namespaces/default/shorturls",
|
||||
Body: []byte(`{ "metadata": { "generateName": "redirect-unified-" }, "spec": { "path": "d/test/unified-redirect" } }`),
|
||||
}, &unstructured.Unstructured{})
|
||||
require.NotNil(t, obj.Result)
|
||||
|
||||
uid := obj.Result.GetName()
|
||||
|
||||
// Test redirect functionality
|
||||
redirectResponse := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodGet,
|
||||
Path: "/goto/" + uid + "?orgId=default",
|
||||
}, (*interface{})(nil))
|
||||
assert.Equal(t, 302, redirectResponse.Response.StatusCode)
|
||||
|
||||
// Clean up
|
||||
err := client.Resource.Delete(context.Background(), uid, metav1.DeleteOptions{})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
// Helper function to check if shortURL K8s APIs are available
|
||||
func checkShortURLAPIAvailable(t *testing.T, helper *apis.K8sTestHelper) bool {
|
||||
disco := helper.NewDiscoveryClient()
|
||||
groups, err := disco.ServerGroups()
|
||||
if err != nil {
|
||||
t.Logf("Failed to get server groups: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
for _, group := range groups.Groups {
|
||||
if group.Name == "shorturl.grafana.app" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// This does a get with both k8s and legacy API, and verifies the results are the same
|
||||
func getFromBothAPIs(t *testing.T,
|
||||
helper *apis.K8sTestHelper,
|
||||
client *apis.K8sResourceClient,
|
||||
uid string,
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
k8sResource, err := client.Resource.Get(context.Background(), uid, metav1.GetOptions{})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, uid, k8sResource.GetName())
|
||||
|
||||
// Legacy API: Try to get the shortURL (might not be implemented)
|
||||
legacyShortURL := apis.DoRequest(helper, apis.RequestParams{
|
||||
User: client.Args.User,
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/short-urls/" + uid,
|
||||
}, &shorturls.ShortUrl{}).Result
|
||||
|
||||
if legacyShortURL != nil {
|
||||
// If legacy API returns data, verify consistency
|
||||
spec, ok := k8sResource.Object["spec"].(map[string]interface{})
|
||||
require.True(t, ok)
|
||||
status, ok := k8sResource.Object["status"].(map[string]interface{})
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, legacyShortURL.Uid, k8sResource.GetName())
|
||||
assert.Equal(t, legacyShortURL.Path, spec["path"].(string))
|
||||
assert.Equal(t, legacyShortURL.LastSeenAt, status["lastSeenAt"].(int64))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user