Skip to content

Commit 32e363c

Browse files
Merge pull request #678 from bergmannf/singleton-ocm-connection
Pass the OCM connection to GetTrustedIPs.
2 parents ea337a6 + d1e607a commit 32e363c

File tree

5 files changed

+19
-24
lines changed

5 files changed

+19
-24
lines changed

cmd/ocm-backplane/cloud/common.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func (cfg *QueryConfig) getIsolatedCredentials(ocmToken string) (aws.Credentials
316316
return aws.Credentials{}, fmt.Errorf("failed to determine client IP: %w", err)
317317
}
318318

319-
trustedRange, err := getTrustedIPList()
319+
trustedRange, err := getTrustedIPList(cfg.OcmConnection)
320320
if err != nil {
321321
return aws.Credentials{}, err
322322
}
@@ -381,8 +381,8 @@ func verifyIPTrusted(ip net.IP, trustedIPs awsutil.IPAddress) error {
381381
return nil
382382
}
383383

384-
func getTrustedIPList() (awsutil.IPAddress, error) {
385-
IPList, err := ocm.DefaultOCMInterface.GetTrustedIPList()
384+
func getTrustedIPList(connection *ocmsdk.Connection) (awsutil.IPAddress, error) {
385+
IPList, err := ocm.DefaultOCMInterface.GetTrustedIPList(connection)
386386
if err != nil {
387387
return awsutil.IPAddress{}, fmt.Errorf("failed to fetch trusted IP list: %w", err)
388388
}

cmd/ocm-backplane/cloud/common_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ var _ = Describe("getIsolatedCredentials", func() {
142142
ip2 := cmv1.NewTrustedIp().ID("200.20.20.20").Enabled(true)
143143
expectedIPList, err := cmv1.NewTrustedIpList().Items(ip1, ip2).Build()
144144
Expect(err).To(BeNil())
145-
mockOcmInterface.EXPECT().GetTrustedIPList().Return(expectedIPList, nil)
145+
mockOcmInterface.EXPECT().GetTrustedIPList(gomock.Any()).Return(expectedIPList, nil)
146146

147147
StsClient = func(proxyURL *string) (*sts.Client, error) {
148148
return &sts.Client{}, nil
@@ -263,8 +263,8 @@ var _ = Describe("getIsolatedCredentials", func() {
263263
ip2 := cmv1.NewTrustedIp().ID("200.20.20.20").Enabled(true)
264264
expectedIPList, err := cmv1.NewTrustedIpList().Items(ip1, ip2).Build()
265265
Expect(err).To(BeNil())
266-
mockOcmInterface.EXPECT().GetTrustedIPList().Return(expectedIPList, nil)
267-
IPList, _ := getTrustedIPList()
266+
mockOcmInterface.EXPECT().GetTrustedIPList(gomock.Any()).Return(expectedIPList, nil)
267+
IPList, _ := getTrustedIPList(testQueryConfig.OcmConnection)
268268
policy, _ := getTrustedIPInlinePolicy(IPList)
269269
//Only allow 209 IP
270270
Expect(policy).To(ContainSubstring("209.10.10.10"))

pkg/ocm/mocks/ocmWrapperMock.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ocm/ocm.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type OCMInterface interface {
3737
GetClusterActiveAccessRequest(ocmConnection *ocmsdk.Connection, clusterID string) (*acctrspv1.AccessRequest, error)
3838
CreateClusterAccessRequest(ocmConnection *ocmsdk.Connection, clusterID, reason, jiraIssueID, approvalDuration string) (*acctrspv1.AccessRequest, error)
3939
CreateAccessRequestDecision(ocmConnection *ocmsdk.Connection, accessRequest *acctrspv1.AccessRequest, decision acctrspv1.DecisionDecision, justification string) (*acctrspv1.Decision, error)
40-
GetTrustedIPList() (*cmv1.TrustedIpList, error)
40+
GetTrustedIPList(*ocmsdk.Connection) (*cmv1.TrustedIpList, error)
4141
SetupOCMConnection() (*ocmsdk.Connection, error)
4242
}
4343

@@ -54,7 +54,6 @@ var DefaultOCMInterface OCMInterface = &DefaultOCMInterfaceImpl{}
5454

5555
// SetupOCMConnection setups the ocm connection for all the other ocm requests
5656
func (o *DefaultOCMInterfaceImpl) SetupOCMConnection() (*ocmsdk.Connection, error) {
57-
5857
envURL := os.Getenv("OCM_URL")
5958
if envURL != "" {
6059
// Fetch the real ocm url from the alias and set it back to the ENV
@@ -478,14 +477,7 @@ func (o *DefaultOCMInterfaceImpl) CreateAccessRequestDecision(ocmConnection *ocm
478477
return accessRequestDecision, nil
479478
}
480479

481-
func (o *DefaultOCMInterfaceImpl) GetTrustedIPList() (*cmv1.TrustedIpList, error) {
482-
// Create the client for the OCM API
483-
connection, err := o.SetupOCMConnection()
484-
if err != nil {
485-
return nil, fmt.Errorf("failed to create OCM connection: %v", err)
486-
}
487-
defer connection.Close()
488-
480+
func (o *DefaultOCMInterfaceImpl) GetTrustedIPList(connection *ocmsdk.Connection) (*cmv1.TrustedIpList, error) {
489481
responseTrustedIP, err := connection.ClustersMgmt().V1().TrustedIPAddresses().List().Send()
490482
if err != nil {
491483

pkg/ocm/ocm_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/golang/mock/gomock"
55
. "github.com/onsi/ginkgo/v2"
66
. "github.com/onsi/gomega"
7+
ocmsdk "github.com/openshift-online/ocm-sdk-go"
78
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
89
"github.com/openshift/backplane-cli/pkg/ocm/mocks"
910
)
@@ -12,11 +13,13 @@ var _ = Describe("OCM Wrapper Test", func() {
1213
var (
1314
ctrl *gomock.Controller
1415
mockOcmInterface *mocks.MockOCMInterface
16+
ocmConnection *ocmsdk.Connection
1517
)
1618

1719
BeforeEach(func() {
1820
ctrl = gomock.NewController(GinkgoT()) // Initialize the controller
1921
mockOcmInterface = mocks.NewMockOCMInterface(ctrl)
22+
ocmConnection = &ocmsdk.Connection{}
2023
})
2124

2225
AfterEach(func() {
@@ -28,16 +31,16 @@ var _ = Describe("OCM Wrapper Test", func() {
2831
ip1 := cmv1.NewTrustedIp().ID("100.10.10.10")
2932
ip2 := cmv1.NewTrustedIp().ID("200.20.20.20")
3033
expectedIPList, _ := cmv1.NewTrustedIpList().Items(ip1, ip2).Build()
31-
mockOcmInterface.EXPECT().GetTrustedIPList().Return(expectedIPList, nil).AnyTimes()
34+
mockOcmInterface.EXPECT().GetTrustedIPList(gomock.Any()).Return(expectedIPList, nil).AnyTimes()
3235

33-
IPList, err := mockOcmInterface.GetTrustedIPList()
36+
IPList, err := mockOcmInterface.GetTrustedIPList(ocmConnection)
3437
Expect(err).To(BeNil())
3538
Expect(len(IPList.Items())).Should(Equal(2))
3639
})
3740

3841
It("Should not return errors for empty trusted IPList", func() {
39-
mockOcmInterface.EXPECT().GetTrustedIPList().Return(nil, nil).AnyTimes()
40-
_, err := mockOcmInterface.GetTrustedIPList()
42+
mockOcmInterface.EXPECT().GetTrustedIPList(gomock.Any()).Return(nil, nil).AnyTimes()
43+
_, err := mockOcmInterface.GetTrustedIPList(ocmConnection)
4144
Expect(err).To(BeNil())
4245
})
4346
})

0 commit comments

Comments
 (0)