-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rpc): Opt-in HTTP RPC API Authorization (#10218)
Context: #10187 Co-authored-by: Marcin Rataj <[email protected]>
- Loading branch information
Showing
12 changed files
with
463 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package auth | ||
|
||
import "net/http" | ||
|
||
var _ http.RoundTripper = &AuthorizedRoundTripper{} | ||
|
||
type AuthorizedRoundTripper struct { | ||
authorization string | ||
roundTripper http.RoundTripper | ||
} | ||
|
||
// NewAuthorizedRoundTripper creates a new [http.RoundTripper] that will set the | ||
// Authorization HTTP header with the value of [authorization]. The given [roundTripper] is | ||
// the base [http.RoundTripper]. If it is nil, [http.DefaultTransport] is used. | ||
func NewAuthorizedRoundTripper(authorization string, roundTripper http.RoundTripper) http.RoundTripper { | ||
if roundTripper == nil { | ||
roundTripper = http.DefaultTransport | ||
} | ||
|
||
return &AuthorizedRoundTripper{ | ||
authorization: authorization, | ||
roundTripper: roundTripper, | ||
} | ||
} | ||
|
||
func (tp *AuthorizedRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { | ||
r.Header.Set("Authorization", tp.authorization) | ||
return tp.roundTripper.RoundTrip(r) | ||
} |
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
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 |
---|---|---|
@@ -1,5 +1,63 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/base64" | ||
"strings" | ||
) | ||
|
||
const ( | ||
APITag = "API" | ||
AuthorizationTag = "Authorizations" | ||
) | ||
|
||
type RPCAuthScope struct { | ||
// AuthSecret is the secret that will be compared to the HTTP "Authorization". | ||
// header. A secret is in the format "type:value". Check the documentation for | ||
// supported types. | ||
AuthSecret string | ||
|
||
// AllowedPaths is an explicit list of RPC path prefixes to allow. | ||
// By default, none are allowed. ["/api/v0"] exposes all RPCs. | ||
AllowedPaths []string | ||
} | ||
|
||
type API struct { | ||
HTTPHeaders map[string][]string // HTTP headers to return with the API. | ||
// HTTPHeaders are the HTTP headers to return with the API. | ||
HTTPHeaders map[string][]string | ||
|
||
// Authorization is a map of authorizations used to authenticate in the API. | ||
// If the map is empty, then the RPC API is exposed to everyone. Check the | ||
// documentation for more details. | ||
Authorizations map[string]*RPCAuthScope `json:",omitempty"` | ||
} | ||
|
||
// ConvertAuthSecret converts the given secret in the format "type:value" into an | ||
// HTTP Authorization header value. It can handle 'bearer' and 'basic' as type. | ||
// If type exists and is not known, an empty string is returned. If type does not | ||
// exist, 'bearer' type is assumed. | ||
func ConvertAuthSecret(secret string) string { | ||
if secret == "" { | ||
return secret | ||
} | ||
|
||
split := strings.SplitN(secret, ":", 2) | ||
if len(split) < 2 { | ||
// No prefix: assume bearer token. | ||
return "Bearer " + secret | ||
} | ||
|
||
if strings.HasPrefix(secret, "basic:") { | ||
if strings.Contains(split[1], ":") { | ||
// Assume basic:user:password | ||
return "Basic " + base64.StdEncoding.EncodeToString([]byte(split[1])) | ||
} else { | ||
// Assume already base64 encoded. | ||
return "Basic " + split[1] | ||
} | ||
} else if strings.HasPrefix(secret, "bearer:") { | ||
return "Bearer " + split[1] | ||
} | ||
|
||
// Unknown. Type is present, but we can't handle it. | ||
return "" | ||
} |
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 config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConvertAuthSecret(t *testing.T) { | ||
for _, testCase := range []struct { | ||
input string | ||
output string | ||
}{ | ||
{"", ""}, | ||
{"someToken", "Bearer someToken"}, | ||
{"bearer:someToken", "Bearer someToken"}, | ||
{"basic:user:pass", "Basic dXNlcjpwYXNz"}, | ||
{"basic:dXNlcjpwYXNz", "Basic dXNlcjpwYXNz"}, | ||
} { | ||
assert.Equal(t, testCase.output, ConvertAuthSecret(testCase.input)) | ||
} | ||
} |
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
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
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
Oops, something went wrong.