forked from goharbor/harbor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request goharbor#10490 from wy65701436/middleware-readonly
move readonly middleware to new v2 handler
Showing
6 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/goharbor/harbor/src/common/utils/log" | ||
"github.com/goharbor/harbor/src/core/config" | ||
internal_errors "github.com/goharbor/harbor/src/internal/error" | ||
"net/http" | ||
) | ||
|
||
type readonlyHandler struct { | ||
next http.Handler | ||
} | ||
|
||
// ReadOnly middleware reject request when harbor set to readonly | ||
func ReadOnly() func(http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
if config.ReadOnly() { | ||
log.Warningf("The request is prohibited in readonly mode, url is: %s", req.URL.Path) | ||
pkgE := internal_errors.New(nil).WithCode(internal_errors.DENIED).WithMessage("The system is in read only mode. Any modification is prohibited.") | ||
http.Error(rw, internal_errors.NewErrs(pkgE).Error(), http.StatusForbidden) | ||
return | ||
} | ||
next.ServeHTTP(rw, req) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/goharbor/harbor/src/common" | ||
config2 "github.com/goharbor/harbor/src/common/config" | ||
"github.com/goharbor/harbor/src/core/config" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
conf := map[string]interface{}{ | ||
common.ReadOnly: "true", | ||
} | ||
kp := &config2.PresetKeyProvider{Key: "naa4JtarA1Zsc3uY"} | ||
config.InitWithSettings(conf, kp) | ||
result := m.Run() | ||
if result != 0 { | ||
os.Exit(result) | ||
} | ||
} | ||
|
||
func TestReadOnly(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(200) | ||
}) | ||
|
||
// delete | ||
req := httptest.NewRequest(http.MethodDelete, "/readonly1", nil) | ||
rec := httptest.NewRecorder() | ||
ReadOnly()(next).ServeHTTP(rec, req) | ||
assert.Equal(rec.Code, http.StatusForbidden) | ||
|
||
update := map[string]interface{}{ | ||
common.ReadOnly: "false", | ||
} | ||
config.GetCfgManager().UpdateConfig(update) | ||
|
||
req2 := httptest.NewRequest(http.MethodDelete, "/readonly2", nil) | ||
rec2 := httptest.NewRecorder() | ||
ReadOnly()(next).ServeHTTP(rec2, req2) | ||
assert.Equal(rec2.Code, http.StatusOK) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package blob | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httputil" | ||
) | ||
|
||
// NewHandler returns the handler to handler catalog request | ||
func NewHandler(proxy *httputil.ReverseProxy) http.Handler { | ||
return &handler{ | ||
proxy: proxy, | ||
} | ||
} | ||
|
||
type handler struct { | ||
proxy *httputil.ReverseProxy | ||
} | ||
|
||
// ServeHTTP ... | ||
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { | ||
h.proxy.ServeHTTP(w, req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package blob |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters