-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor HTTP client creation to be more generic (#113)
- Loading branch information
1 parent
cd9c996
commit e2aec9a
Showing
10 changed files
with
96 additions
and
80 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package custom | ||
|
||
import "net/http" | ||
|
||
var isOverridden bool | ||
var clientFactoryOverride ClientFactory | ||
var preflightFunctions = make([]RequestPreflight, 0) | ||
|
||
type ClientFactory func() (*http.Client, error) | ||
|
||
func UseCustom() bool { | ||
return isOverridden | ||
} | ||
|
||
func NewHTTPClient() (*http.Client, error) { | ||
return clientFactoryOverride() | ||
} | ||
|
||
// RegisterClientFactory overrides Weep's standard config-based ConsoleMe client | ||
// creation with a ClientFactory. This function will be called during the creation | ||
// of all ConsoleMe clients. | ||
func RegisterClientFactory(factory ClientFactory) { | ||
clientFactoryOverride = factory | ||
isOverridden = true | ||
} | ||
|
||
type RequestPreflight func(req *http.Request) error | ||
|
||
// RegisterRequestPreflight adds a RequestPreflight function which will be called in the | ||
// order of registration during the creation of a ConsoleMe request. | ||
func RegisterRequestPreflight(preflight RequestPreflight) { | ||
preflightFunctions = append(preflightFunctions, preflight) | ||
} | ||
|
||
func RunPreflightFunctions(req *http.Request) error { | ||
var err error | ||
if preflightFunctions != nil { | ||
for _, preflight := range preflightFunctions { | ||
if err = preflight(req); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} |
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,28 @@ | ||
package httpAuth | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/netflix/weep/pkg/httpAuth/challenge" | ||
"github.com/netflix/weep/pkg/httpAuth/custom" | ||
"github.com/netflix/weep/pkg/httpAuth/mtls" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func GetAuthenticatedClient() (*http.Client, error) { | ||
authenticationMethod := viper.GetString("authentication_method") | ||
consoleMeUrl := viper.GetString("consoleme_url") | ||
if custom.UseCustom() { | ||
return custom.NewHTTPClient() | ||
} else if authenticationMethod == "mtls" { | ||
return mtls.NewHTTPClient() | ||
} else if authenticationMethod == "challenge" { | ||
err := challenge.RefreshChallenge() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return challenge.NewHTTPClient(consoleMeUrl) | ||
} | ||
return nil, fmt.Errorf("Authentication method unsupported or not provided.") | ||
} |
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