mirror of https://github.com/harness/drone.git
[feat]: [AH-152]: maven download url fix (#3339)
* [feat]: [AH-152]: maven download url fix * [feat]: [AH-152]: maven download url fix * [feat]: [AH-152]: maven download url fix * [feat]: [AH-152]: maven download url fixpull/3616/head
parent
4eb00eae4c
commit
2990923f15
|
@ -432,7 +432,7 @@ func (c *APIController) generateMavenClientSetupDetail(
|
|||
},
|
||||
{
|
||||
//nolint:lll
|
||||
Header: stringPtr("Copy the following your ~/ .m2/setting.xml file for MacOs, or $USERPROFILE$\\ .m2\\settings.xml for Windows to authenticate with token to pull from your Maven registry."),
|
||||
Header: stringPtr("Copy the following your ~/ .m2/settings.xml file for MacOs, or $USERPROFILE$\\ .m2\\settings.xml for Windows to authenticate with token to pull from your Maven registry."),
|
||||
Type: &staticStepType,
|
||||
Commands: &[]artifact.ClientSetupStepCommand{
|
||||
{
|
||||
|
|
|
@ -404,7 +404,7 @@ func GetGenericArtifactFileDownloadCommand(regURL, artifact, version, filename s
|
|||
|
||||
func GetMavenArtifactFileDownloadCommand(regURL, artifact, version, filename string) string {
|
||||
downloadCommand := "curl --location '<HOSTNAME>/<ARTIFACT>/<VERSION>/<FILENAME>'" +
|
||||
" --header 'Authorization: <IDENTITY_TOKEN>'"
|
||||
" --header 'x-api-key: <IDENTITY_TOKEN>' -O"
|
||||
|
||||
// Replace the placeholders with the actual values
|
||||
replacements := map[string]string{
|
||||
|
|
|
@ -106,6 +106,26 @@ func CheckMavenAuth() func(http.Handler) http.Handler {
|
|||
}
|
||||
}
|
||||
|
||||
func CheckMavenAuthHeader() func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
authHeader := r.Header.Get("Authorization")
|
||||
apiKeyHeader := r.Header.Get("x-api-key")
|
||||
if authHeader == "" && apiKeyHeader == "" {
|
||||
setMavenHeaders(w)
|
||||
render.Unauthorized(ctx, w)
|
||||
return
|
||||
} else if apiKeyHeader != "" {
|
||||
r.Header.Set("Authorization", apiKeyHeader)
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func setMavenHeaders(w http.ResponseWriter) {
|
||||
w.Header().Set("WWW-Authenticate", "Basic realm=\"Harness Registry\"")
|
||||
}
|
||||
|
|
|
@ -184,7 +184,7 @@ func TrackBandwidthStatForMavenArtifacts(h *maven.Handler) func(http.Handler) ht
|
|||
}
|
||||
ctx := r.Context()
|
||||
|
||||
info, err := h.GetArtifactInfo(r, false)
|
||||
info, err := h.GetArtifactInfo(r, true)
|
||||
if !commons.IsEmpty(err) {
|
||||
log.Ctx(ctx).Error().Stack().Str("middleware",
|
||||
"TrackBandwidthStat").Err(err).Msgf("error while putting bandwidth stat for artifact, %v",
|
||||
|
@ -261,6 +261,9 @@ func dbBandwidthStatForMavenArtifact(
|
|||
}
|
||||
|
||||
image, err := c.DBStore.ImageDao.GetByName(ctx, registry.ID, imageName)
|
||||
if errors.Is(err, store.ErrResourceNotFound) {
|
||||
image, err = getMavenArtifactFromUpstreamProxy(ctx, c, info)
|
||||
}
|
||||
if err != nil {
|
||||
return errcode.ErrCodeInvalidRequest.WithDetail(err)
|
||||
}
|
||||
|
@ -270,7 +273,7 @@ func dbBandwidthStatForMavenArtifact(
|
|||
return errcode.ErrCodeInvalidRequest.WithDetail(err)
|
||||
}
|
||||
|
||||
var metadata database.GenericMetadata
|
||||
var metadata database.MavenMetadata
|
||||
err = json.Unmarshal(art.Metadata, &metadata)
|
||||
|
||||
if err != nil {
|
||||
|
@ -278,8 +281,11 @@ func dbBandwidthStatForMavenArtifact(
|
|||
}
|
||||
|
||||
var size int64
|
||||
for _, files := range metadata.Files {
|
||||
size += files.Size
|
||||
for _, file := range metadata.Files {
|
||||
if file.Filename == info.FileName {
|
||||
size = file.Size
|
||||
break
|
||||
}
|
||||
}
|
||||
bandwidthStat := &types.BandwidthStat{
|
||||
ImageID: image.ID,
|
||||
|
@ -344,3 +350,21 @@ func getImageFromUpstreamProxy(ctx context.Context, c *docker.Controller, info p
|
|||
}
|
||||
return nil, errors.New("image not found in upstream proxy")
|
||||
}
|
||||
|
||||
func getMavenArtifactFromUpstreamProxy(ctx context.Context,
|
||||
c *maven2.Controller,
|
||||
info pkg.MavenArtifactInfo,
|
||||
) (*types.Image, error) {
|
||||
repos, err := c.GetOrderedRepos(ctx, info.RegIdentifier, *info.BaseInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, registry := range repos {
|
||||
log.Ctx(ctx).Info().Msgf("Using Repository: %s, Type: %s", registry.Name, registry.Type)
|
||||
image, err := c.DBStore.ImageDao.GetByName(ctx, registry.ID, info.GroupID+":"+info.ArtifactID)
|
||||
if err == nil && image != nil {
|
||||
return image, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("artifact not found in upstream proxy")
|
||||
}
|
||||
|
|
|
@ -188,8 +188,8 @@ func TrackDownloadStatForMavenArtifact(h *maven.Handler) func(http.Handler) http
|
|||
return
|
||||
}
|
||||
|
||||
err = dbDownloadStatForMavenArtifact(ctx, h.Controller, info)
|
||||
if !commons.IsEmpty(err) {
|
||||
err2 := dbDownloadStatForMavenArtifact(ctx, h.Controller, info)
|
||||
if !commons.IsEmptyError(err2) {
|
||||
log.Ctx(ctx).Error().Stack().Str("middleware",
|
||||
"TrackDownloadStat").Err(err).Msgf("error while putting download stat of artifact, %v",
|
||||
err)
|
||||
|
@ -242,6 +242,9 @@ func dbDownloadStatForMavenArtifact(
|
|||
}
|
||||
|
||||
image, err := c.DBStore.ImageDao.GetByName(ctx, registry.ID, imageName)
|
||||
if errors.Is(err, store.ErrResourceNotFound) {
|
||||
image, err = getMavenArtifactFromUpstreamProxy(ctx, c, info)
|
||||
}
|
||||
if err != nil {
|
||||
return errcode.ErrCodeInvalidRequest.WithDetail(err)
|
||||
}
|
||||
|
|
|
@ -40,6 +40,7 @@ func NewMavenHandler(handler *maven.Handler) Handler {
|
|||
|
||||
r.Route("/maven", func(r chi.Router) {
|
||||
r.Use(middleware.StoreOriginalURL)
|
||||
r.Use(middleware.CheckMavenAuthHeader())
|
||||
r.Use(middlewareauthn.Attempt(handler.Authenticator))
|
||||
r.Use(middleware.CheckMavenAuth())
|
||||
r.Use(middleware.TrackDownloadStatForMavenArtifact(handler))
|
||||
|
|
Loading…
Reference in New Issue