Skip to content

Commit

Permalink
feat: cleanup Gateway.HTTPHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Aug 28, 2023
1 parent 5d24f2e commit 4dbea75
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
36 changes: 36 additions & 0 deletions fs-repo-14-to-15/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,42 @@ func convert(in io.Reader, out io.Writer) error {
}
}

// Remove legacy Gateway.HTTPHeaders values that were hardcoded since years ago, but no longer necessary
// (but leave as-is if user made any changes)
// https://github.com/ipfs/kubo/issues/10005
if a, ok := confMap["Gateway"]; ok {
addresses, ok := a.(map[string]any)
if !ok {
return fmt.Errorf("invalid type for .Gateway got %T expected json map", a)
}

if s, ok := addresses["HTTPHeaders"]; ok {
headers, ok := s.(map[string]any)
if !ok {
return fmt.Errorf("invalid type for .Gateway.HTTPHeaders got %T expected json map", s)
}

if acaos, ok := headers["Access-Control-Allow-Origin"].([]interface{}); ok && len(acaos) == 1 && acaos[0] == "*" {
delete(headers, "Access-Control-Allow-Origin")
} else {
return fmt.Errorf("invalid type for .Gateway.HTTPHeaders[Access-Control-Allow-Origin] got %T expected json array", headers["Access-Control-Allow-Origin"])
}

if acams, ok := headers["Access-Control-Allow-Methods"].([]interface{}); ok && len(acams) == 1 && acams[0] == "GET" {
delete(headers, "Access-Control-Allow-Methods")
} else {
return fmt.Errorf("invalid type for .Gateway.HTTPHeaders[Access-Control-Allow-Methods] got %T expected json array", headers["Access-Control-Allow-Methods"])
}
if acahs, ok := headers["Access-Control-Allow-Headers"].([]interface{}); ok && len(acahs) == 3 {
if acahs[0] == "X-Requested-With" && acahs[1] == "Range" && acahs[2] == "User-Agent" {
delete(headers, "Access-Control-Allow-Headers")
}
} else {
return fmt.Errorf("invalid type for .Gateway.HTTPHeaders[Access-Control-Allow-Headers] got %T expected json array", headers["Access-Control-Allow-Headers"])
}
}
}

// Save new config
fixed, err := json.MarshalIndent(confMap, "", " ")
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion fs-repo-14-to-15/not-sharness/repotest-golden/config
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb"
]
],
"Gateway": {
"HTTPHeaders": {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,20 @@
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb"
]
],
"Gateway": {
"HTTPHeaders": {
"Access-Control-Allow-Headers": [
"X-Requested-With",
"Range",
"User-Agent"
],
"Access-Control-Allow-Methods": [
"GET"
],
"Access-Control-Allow-Origin": [
"*"
]
}
}
}
17 changes: 16 additions & 1 deletion fs-repo-14-to-15/not-sharness/repotest-init/config
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,20 @@
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb"
]
],
"Gateway": {
"HTTPHeaders": {
"Access-Control-Allow-Headers": [
"X-Requested-With",
"Range",
"User-Agent"
],
"Access-Control-Allow-Methods": [
"GET"
],
"Access-Control-Allow-Origin": [
"*"
]
}
}
}
1 change: 1 addition & 0 deletions ignored-migrations
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ fs-repo-8-to-9
fs-repo-9-to-10
fs-repo-10-to-11
fs-repo-11-to-12
fs-repo-12-to-13

0 comments on commit 4dbea75

Please sign in to comment.