mirror of
https://github.com/harness/drone.git
synced 2025-05-05 23:42:57 +00:00
feat: [AH-1197]: implement delete image for non oci flow (#3700)
* feat: [AH-1197]: implement delete image for non oci flow
This commit is contained in:
parent
d361fc3f5a
commit
646cb57578
@ -23,7 +23,7 @@ import (
|
|||||||
"github.com/harness/gitness/app/api/request"
|
"github.com/harness/gitness/app/api/request"
|
||||||
"github.com/harness/gitness/audit"
|
"github.com/harness/gitness/audit"
|
||||||
"github.com/harness/gitness/registry/app/api/openapi/contracts/artifact"
|
"github.com/harness/gitness/registry/app/api/openapi/contracts/artifact"
|
||||||
registryTypes "github.com/harness/gitness/registry/types"
|
"github.com/harness/gitness/registry/app/api/utils"
|
||||||
"github.com/harness/gitness/types/enum"
|
"github.com/harness/gitness/types/enum"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@ -65,84 +65,127 @@ func (c *APIController) DeleteArtifact(ctx context.Context, r artifact.DeleteArt
|
|||||||
}
|
}
|
||||||
|
|
||||||
repoEntity, err := c.RegistryRepository.GetByParentIDAndName(ctx, regInfo.parentID, regInfo.RegistryIdentifier)
|
repoEntity, err := c.RegistryRepository.GetByParentIDAndName(ctx, regInfo.parentID, regInfo.RegistryIdentifier)
|
||||||
if len(repoEntity.Name) == 0 {
|
if err != nil {
|
||||||
|
//nolint:nilerr
|
||||||
return artifact.DeleteArtifact404JSONResponse{
|
return artifact.DeleteArtifact404JSONResponse{
|
||||||
NotFoundJSONResponse: artifact.NotFoundJSONResponse(
|
NotFoundJSONResponse: artifact.NotFoundJSONResponse(
|
||||||
*GetErrorResponse(http.StatusNotFound, "registry doesn't exist with this key"),
|
*GetErrorResponse(http.StatusNotFound, "registry doesn't exist with this key"),
|
||||||
),
|
),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
return throwDeleteArtifact500Error(err), err
|
|
||||||
}
|
|
||||||
|
|
||||||
artifactName := string(r.Artifact)
|
artifactName := string(r.Artifact)
|
||||||
artifactDetails, err := c.ImageStore.GetByName(ctx, regInfo.RegistryID, artifactName)
|
_, err = c.ImageStore.GetByName(ctx, regInfo.RegistryID, artifactName)
|
||||||
if err != nil || artifactDetails == nil {
|
if err != nil {
|
||||||
|
//nolint:nilerr
|
||||||
return artifact.DeleteArtifact404JSONResponse{
|
return artifact.DeleteArtifact404JSONResponse{
|
||||||
NotFoundJSONResponse: artifact.NotFoundJSONResponse(
|
NotFoundJSONResponse: artifact.NotFoundJSONResponse(
|
||||||
*GetErrorResponse(http.StatusNotFound, "artifact doesn't exist with this key"),
|
*GetErrorResponse(http.StatusNotFound, "artifact doesn't exist with this key"),
|
||||||
),
|
),
|
||||||
}, err
|
|
||||||
}
|
|
||||||
if !artifactDetails.Enabled {
|
|
||||||
return artifact.DeleteArtifact404JSONResponse{
|
|
||||||
NotFoundJSONResponse: artifact.NotFoundJSONResponse(
|
|
||||||
*GetErrorResponse(http.StatusNotFound, "artifact is already deleted"),
|
|
||||||
),
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
err = c.tx.WithTx(
|
|
||||||
ctx, func(ctx context.Context) error {
|
|
||||||
err = c.disableImageStatus(
|
|
||||||
ctx, regInfo, artifactName,
|
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
switch regInfo.PackageType {
|
||||||
return fmt.Errorf("failed to delete artifact: %w", err)
|
case artifact.PackageTypeDOCKER:
|
||||||
}
|
err = c.deleteOCIImage(ctx, regInfo, artifactName)
|
||||||
|
case artifact.PackageTypeHELM:
|
||||||
err := c.TagStore.DeleteTagsByImageName(ctx, regInfo.RegistryID, artifactName)
|
err = c.deleteOCIImage(ctx, regInfo, artifactName)
|
||||||
|
case artifact.PackageTypeGENERIC:
|
||||||
if err != nil {
|
err = c.deleteGenericImage(ctx, regInfo, artifactName)
|
||||||
return fmt.Errorf("failed to delete artifact: %w", err)
|
case artifact.PackageTypeMAVEN:
|
||||||
}
|
err = c.deleteGenericImage(ctx, regInfo, artifactName)
|
||||||
|
case artifact.PackageTypePYTHON:
|
||||||
auditErr := c.AuditService.Log(
|
err = c.deleteGenericImage(ctx, regInfo, artifactName)
|
||||||
ctx,
|
case artifact.PackageTypeNPM:
|
||||||
session.Principal,
|
err = c.deleteGenericImage(ctx, regInfo, artifactName)
|
||||||
audit.NewResource(audit.ResourceTypeRegistryArtifact, string(r.Artifact)),
|
case artifact.PackageTypeNUGET:
|
||||||
audit.ActionDeleted,
|
err = fmt.Errorf("delete artifact not supported for nuget")
|
||||||
regInfo.ParentRef,
|
case artifact.PackageTypeRPM:
|
||||||
audit.WithData("registry name", repoEntity.Name),
|
err = fmt.Errorf("delete artifact not supported for rpm")
|
||||||
audit.WithData("artifact name", string(r.Artifact)),
|
default:
|
||||||
)
|
err = fmt.Errorf("unsupported package type: %s", regInfo.PackageType)
|
||||||
if auditErr != nil {
|
}
|
||||||
log.Ctx(ctx).Warn().Msgf("failed to insert audit log for delete tag operation: %s", auditErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return throwDeleteArtifact500Error(err), err
|
return throwDeleteArtifact500Error(err), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auditErr := c.AuditService.Log(
|
||||||
|
ctx,
|
||||||
|
session.Principal,
|
||||||
|
audit.NewResource(audit.ResourceTypeRegistryArtifact, string(r.Artifact)),
|
||||||
|
audit.ActionDeleted,
|
||||||
|
regInfo.ParentRef,
|
||||||
|
audit.WithData("registry name", repoEntity.Name),
|
||||||
|
audit.WithData("artifact name", string(r.Artifact)),
|
||||||
|
)
|
||||||
|
if auditErr != nil {
|
||||||
|
log.Ctx(ctx).Warn().Msgf("failed to insert audit log for delete tag operation: %s", auditErr)
|
||||||
|
}
|
||||||
|
|
||||||
return artifact.DeleteArtifact200JSONResponse{
|
return artifact.DeleteArtifact200JSONResponse{
|
||||||
SuccessJSONResponse: artifact.SuccessJSONResponse(*GetSuccessResponse()),
|
SuccessJSONResponse: artifact.SuccessJSONResponse(*GetSuccessResponse()),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *APIController) disableImageStatus(
|
func (c *APIController) deleteOCIImage(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
regInfo *RegistryRequestBaseInfo, artifactName string,
|
regInfo *RegistryRequestBaseInfo,
|
||||||
|
artifactName string,
|
||||||
) error {
|
) error {
|
||||||
image := ®istryTypes.Image{
|
err := c.tx.WithTx(
|
||||||
Name: artifactName,
|
ctx, func(ctx context.Context) error {
|
||||||
RegistryID: regInfo.RegistryID,
|
// Delete tags linked to the image
|
||||||
Enabled: false,
|
err := c.TagStore.DeleteTagsByImageName(ctx, regInfo.RegistryID, artifactName)
|
||||||
}
|
if err != nil {
|
||||||
err := c.ImageStore.UpdateStatus(ctx, image)
|
return fmt.Errorf("failed to delete artifact: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete image
|
||||||
|
err = c.ImageStore.DeleteByImageNameAndRegID(
|
||||||
|
ctx, regInfo.RegistryID, artifactName,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to delete artifact: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *APIController) deleteGenericImage(
|
||||||
|
ctx context.Context,
|
||||||
|
regInfo *RegistryRequestBaseInfo,
|
||||||
|
artifactName string,
|
||||||
|
) error {
|
||||||
|
err := c.tx.WithTx(
|
||||||
|
ctx, func(ctx context.Context) error {
|
||||||
|
// Get File Path
|
||||||
|
filePath, err := utils.GetFilePath(regInfo.PackageType, artifactName, "")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get file path: %w", err)
|
||||||
|
}
|
||||||
|
// Delete Artifact Files
|
||||||
|
err = c.fileManager.DeleteNode(ctx, regInfo.RegistryID, filePath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to delete artifact files: %w", err)
|
||||||
|
}
|
||||||
|
// Delete Artifacts
|
||||||
|
err = c.ArtifactStore.DeleteByImageNameAndRegistryID(ctx, regInfo.RegistryID, artifactName)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to delete versions: %w", err)
|
||||||
|
}
|
||||||
|
// Delete image
|
||||||
|
err = c.ImageStore.DeleteByImageNameAndRegID(
|
||||||
|
ctx, regInfo.RegistryID, artifactName,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to delete artifact: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,16 +70,14 @@ func (c *APIController) DeleteRegistry(
|
|||||||
}
|
}
|
||||||
|
|
||||||
repoEntity, err := c.RegistryRepository.GetByParentIDAndName(ctx, regInfo.parentID, regInfo.RegistryIdentifier)
|
repoEntity, err := c.RegistryRepository.GetByParentIDAndName(ctx, regInfo.parentID, regInfo.RegistryIdentifier)
|
||||||
if len(repoEntity.Name) == 0 {
|
if err != nil {
|
||||||
|
//nolint:nilerr
|
||||||
return artifact.DeleteRegistry404JSONResponse{
|
return artifact.DeleteRegistry404JSONResponse{
|
||||||
NotFoundJSONResponse: artifact.NotFoundJSONResponse(
|
NotFoundJSONResponse: artifact.NotFoundJSONResponse(
|
||||||
*GetErrorResponse(http.StatusNotFound, "registry doesn't exist with this key"),
|
*GetErrorResponse(http.StatusNotFound, "registry doesn't exist with this key"),
|
||||||
),
|
),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
return throwDeleteRegistry500Error(err), err
|
|
||||||
}
|
|
||||||
|
|
||||||
if string(repoEntity.Type) == string(artifact.RegistryTypeVIRTUAL) {
|
if string(repoEntity.Type) == string(artifact.RegistryTypeVIRTUAL) {
|
||||||
err = c.tx.WithTx(
|
err = c.tx.WithTx(
|
||||||
@ -196,17 +194,7 @@ func (c *APIController) deleteRegistryWithAudit(
|
|||||||
ctx context.Context, regInfo *RegistryRequestBaseInfo,
|
ctx context.Context, regInfo *RegistryRequestBaseInfo,
|
||||||
registry *registrytypes.Registry, principal types.Principal, parentRef string,
|
registry *registrytypes.Registry, principal types.Principal, parentRef string,
|
||||||
) error {
|
) error {
|
||||||
err := c.ImageStore.DeleteDownloadStatByRegistryID(ctx, regInfo.RegistryID)
|
err := c.ImageStore.DeleteByRegistryID(ctx, regInfo.RegistryID)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = c.ImageStore.DeleteBandwidthStatByRegistryID(ctx, regInfo.RegistryID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = c.ImageStore.DeleteByRegistryID(ctx, regInfo.RegistryID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user