Skip to content

Commit

Permalink
microcloud/service: Use UseAuthProxy for microcluster clients
Browse files Browse the repository at this point in the history
Signed-off-by: Max Asnaashari <[email protected]>
  • Loading branch information
masnax committed Apr 19, 2024
1 parent c37d4c4 commit dcc57dd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 61 deletions.
28 changes: 7 additions & 21 deletions microcloud/service/microceph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package service

import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/url"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/canonical/microcluster/microcluster"

"github.com/canonical/microcloud/microcloud/api/types"
cloudCli "github.com/canonical/microcloud/microcloud/client"
)

// CephService is a MicroCeph service.
Expand Down Expand Up @@ -62,15 +62,9 @@ func (s CephService) Client(target string, secret string) (*client.Client, error
c = c.UseTarget(target)
}

if secret != "" {
c.Client.Client.Transport.(*http.Transport).Proxy = func(r *http.Request) (*url.URL, error) {
r.Header.Set("X-MicroCloud-Auth", secret)
if !strings.HasPrefix(r.URL.Path, "/1.0/services/microceph") {
r.URL.Path = "/1.0/services/microceph" + r.URL.Path
}

return shared.ProxyFromEnvironment(r)
}
c, err = cloudCli.UseAuthProxy(c, secret, types.MicroCeph)
if err != nil {
return nil, err
}

return c, nil
Expand Down Expand Up @@ -138,17 +132,9 @@ func (s CephService) RemoteClusterMembers(ctx context.Context, secret string, ad
return nil, err
}

client.Client.Client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
Proxy: func(r *http.Request) (*url.URL, error) {
r.Header.Set("X-MicroCloud-Auth", secret)
if !strings.HasPrefix(r.URL.Path, "/1.0/services/microceph") {
r.URL.Path = "/1.0/services/microceph" + r.URL.Path
}

return shared.ProxyFromEnvironment(r)
},
client, err = cloudCli.UseAuthProxy(client, secret, types.MicroCeph)
if err != nil {
return nil, err
}

return clusterMembers(ctx, client)
Expand Down
38 changes: 10 additions & 28 deletions microcloud/service/microcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package service

import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/canonical/lxd/lxd/util"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
cephTypes "github.com/canonical/microceph/microceph/api/types"
microClient "github.com/canonical/microcluster/client"
Expand All @@ -21,6 +17,7 @@ import (

"github.com/canonical/microcloud/microcloud/api/types"
"github.com/canonical/microcloud/microcloud/client"
cloudCli "github.com/canonical/microcloud/microcloud/client"
)

// CloudService is a MicroCloud service.
Expand Down Expand Up @@ -103,14 +100,9 @@ func (s CloudService) RemoteIssueToken(ctx context.Context, clusterAddress strin
return "", err
}

c.Client.Client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
Proxy: func(r *http.Request) (*url.URL, error) {
r.Header.Set("X-MicroCloud-Auth", secret)

return shared.ProxyFromEnvironment(r)
},
c, err = cloudCli.UseAuthProxy(c, secret, types.MicroCloud)
if err != nil {
return "", err
}

return client.RemoteIssueToken(ctx, c, serviceType, types.ServiceTokensPost{ClusterAddress: c.URL().URL.Host, JoinerName: peer})
Expand Down Expand Up @@ -139,14 +131,9 @@ func (s CloudService) RequestJoin(ctx context.Context, secret string, name strin
return err
}

c.Client.Client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
Proxy: func(r *http.Request) (*url.URL, error) {
r.Header.Set("X-MicroCloud-Auth", secret)

return shared.ProxyFromEnvironment(r)
},
c, err = cloudCli.UseAuthProxy(c, secret, types.MicroCloud)
if err != nil {
return err
}
}

Expand All @@ -160,14 +147,9 @@ func (s CloudService) RemoteClusterMembers(ctx context.Context, secret string, a
return nil, err
}

client.Client.Client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
Proxy: func(r *http.Request) (*url.URL, error) {
r.Header.Set("X-MicroCloud-Auth", secret)

return shared.ProxyFromEnvironment(r)
},
client, err = cloudCli.UseAuthProxy(client, secret, types.MicroCloud)
if err != nil {
return nil, err
}

return clusterMembers(ctx, client)
Expand Down
16 changes: 4 additions & 12 deletions microcloud/service/microovn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package service

import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/url"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/canonical/microcluster/microcluster"

"github.com/canonical/microcloud/microcloud/api/types"
cloudCli "github.com/canonical/microcloud/microcloud/client"
)

// OVNService is a MicroOVN service.
Expand Down Expand Up @@ -99,17 +99,9 @@ func (s OVNService) RemoteClusterMembers(ctx context.Context, secret string, add
return nil, err
}

client.Client.Client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
Proxy: func(r *http.Request) (*url.URL, error) {
r.Header.Set("X-MicroCloud-Auth", secret)
if !strings.HasPrefix(r.URL.Path, "/1.0/services/microovn") {
r.URL.Path = "/1.0/services/microovn" + r.URL.Path
}

return shared.ProxyFromEnvironment(r)
},
client, err = cloudCli.UseAuthProxy(client, secret, types.MicroOVN)
if err != nil {
return nil, err
}

return clusterMembers(ctx, client)
Expand Down

0 comments on commit dcc57dd

Please sign in to comment.