Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mtls refactor for endpoints #9382

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/4869.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
added support for [mtls authentication](https://google.aip.dev/auth/4114)
```
372 changes: 233 additions & 139 deletions google/config.go

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions google/mtls_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package google

import (
"context"
"fmt"
"net/url"
"strings"

"google.golang.org/api/option/internaloption"
"google.golang.org/api/transport"
)

// The transport libaray does not natively expose logic to determine whether
// the user is within mtls mode or not. They do return the mtls endpoint if
// it is enabled during client creation so we will use this logic to determine
// the mode the user is in and throw away the client they give us back.
func isMtls() bool {
regularEndpoint := "https://mockservice.googleapis.com/v1/"
mtlsEndpoint := getMtlsEndpoint(regularEndpoint)
_, endpoint, err := transport.NewHTTPClient(context.Background(),
internaloption.WithDefaultEndpoint(regularEndpoint),
internaloption.WithDefaultMTLSEndpoint(mtlsEndpoint),
)
if err != nil {
return false
}
isMtls := endpoint == mtlsEndpoint
return isMtls
}

func getMtlsEndpoint(baseEndpoint string) string {
u, err := url.Parse(baseEndpoint)
if err != nil {
if strings.Contains(baseEndpoint, ".googleapis") {
return strings.Replace(baseEndpoint, ".googleapis", ".mtls.googleapis", 1)
}
return baseEndpoint
}
domainParts := strings.Split(u.Host, ".")
if len(domainParts) > 1 {
u.Host = fmt.Sprintf("%s.mtls.%s", domainParts[0], strings.Join(domainParts[1:], "."))
} else {
u.Host = fmt.Sprintf("%s.mtls", domainParts[0])
}
return u.String()
}
16 changes: 16 additions & 0 deletions google/mtls_util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package google

import (
"strings"
"testing"
)

func TestUnitMtls_urlSwitching(t *testing.T) {
t.Parallel()
for key, bp := range DefaultBasePaths {
url := getMtlsEndpoint(bp)
if !strings.Contains(url, ".mtls.") {
t.Errorf("%s: mtls conversion unsuccessful preconv - %s postconv - %s", key, bp, url)
}
}
}
Loading