Skip to content

Commit

Permalink
Turn sublease header into const
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSandro2000 committed Apr 3, 2024
1 parent 79682c8 commit b9182ba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/api/main.go
Expand Up @@ -90,7 +90,7 @@ func run(cmd *cobra.Command, args []string) {
corsMiddleware := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"HEAD", "GET", "POST", "PUT", "DELETE"},
AllowedHeaders: []string{"Content-Type", "User-Agent", "Authorization", "X-Auth-Token", "X-Keppel-Sublease-Token"},
AllowedHeaders: []string{"Content-Type", "User-Agent", "Authorization", "X-Auth-Token", keppel.SubleaseHeader},
})
handler := httpapi.Compose(
keppelv1.NewAPI(cfg, ad, fd, sd, icd, db, auditor, rle),
Expand Down
20 changes: 10 additions & 10 deletions internal/api/keppel/accounts_test.go
Expand Up @@ -1291,8 +1291,8 @@ func TestGetPutAccountReplicationOnFirstUse(t *testing.T) {
Method: "PUT",
Path: "/keppel/v1/accounts/first",
Header: map[string]string{
"X-Test-Perms": "change:tenant1",
"X-Keppel-Sublease-Token": makeSubleaseToken("first", "registry.example.org", "not-the-valid-token"),
"X-Test-Perms": "change:tenant1",
keppel.SubleaseHeader: makeSubleaseToken("first", "registry.example.org", "not-the-valid-token"),
},
Body: assert.JSONObject{
"account": assert.JSONObject{
Expand All @@ -1312,8 +1312,8 @@ func TestGetPutAccountReplicationOnFirstUse(t *testing.T) {
Method: "PUT",
Path: "/keppel/v1/accounts/first",
Header: map[string]string{
"X-Test-Perms": "change:tenant1",
"X-Keppel-Sublease-Token": makeSubleaseToken("first", "registry.example.org", "valid-token"),
"X-Test-Perms": "change:tenant1",
keppel.SubleaseHeader: makeSubleaseToken("first", "registry.example.org", "valid-token"),
},
Body: assert.JSONObject{
"account": assert.JSONObject{
Expand Down Expand Up @@ -2137,8 +2137,8 @@ func TestReplicaAccountsInheritPlatformFilter(t *testing.T) {
Method: "PUT",
Path: "/keppel/v1/accounts/first",
Header: map[string]string{
"X-Test-Perms": "change:tenant1",
"X-Keppel-Sublease-Token": makeSubleaseToken("first", "registry.example.org", "valid-token"),
"X-Test-Perms": "change:tenant1",
SubleaseHeader: makeSubleaseToken("first", "registry.example.org", "valid-token"),
},
Body: assert.JSONObject{
"account": assert.JSONObject{
Expand Down Expand Up @@ -2171,8 +2171,8 @@ func TestReplicaAccountsInheritPlatformFilter(t *testing.T) {
Method: "PUT",
Path: "/keppel/v1/accounts/second",
Header: map[string]string{
"X-Test-Perms": "change:tenant1",
"X-Keppel-Sublease-Token": makeSubleaseToken("second", "registry.example.org", "valid-token"),
"X-Test-Perms": "change:tenant1",
keppel.SubleaseHeader: makeSubleaseToken("second", "registry.example.org", "valid-token"),
},
Body: assert.JSONObject{
"account": assert.JSONObject{
Expand Down Expand Up @@ -2209,8 +2209,8 @@ func TestReplicaAccountsInheritPlatformFilter(t *testing.T) {
Method: "PUT",
Path: "/keppel/v1/accounts/third",
Header: map[string]string{
"X-Test-Perms": "change:tenant1",
"X-Keppel-Sublease-Token": makeSubleaseToken("third", "registry.example.org", "valid-token"),
"X-Test-Perms": "change:tenant1",
keppel.SubleaseHeader: makeSubleaseToken("third", "registry.example.org", "valid-token"),
},
Body: assert.JSONObject{
"account": assert.JSONObject{
Expand Down
6 changes: 4 additions & 2 deletions internal/api/keppel/sublease.go
Expand Up @@ -25,6 +25,8 @@ import (
"net/http"
)

const SubleaseHeader = "X-Keppel-Sublease-Token"

// SubleaseToken is the internal structure of a sublease token. Only the secret
// is passed on to the federation driver. The other attributes are only
// informational. GUIs/CLIs can display these data to the user for confirmation
Expand All @@ -51,13 +53,13 @@ func SubleaseTokenFromRequest(r *http.Request) (SubleaseToken, error) {

buf, err := base64.StdEncoding.DecodeString(in)
if err != nil {
return SubleaseToken{}, fmt.Errorf("malformed X-Keppel-Sublease-Token header: %s", err.Error())
return SubleaseToken{}, fmt.Errorf("malformed %s header: %w", SubleaseHeader, err)
}

var t SubleaseToken
err = json.Unmarshal(buf, &t)
if err != nil {
return SubleaseToken{}, fmt.Errorf("malformed X-Keppel-Sublease-Token header: %s", err.Error())
return SubleaseToken{}, fmt.Errorf("malformed %s header: %w", SubleaseHeader, err)
}
return t, nil
}

0 comments on commit b9182ba

Please sign in to comment.