Skip to content

Commit a6a39f3

Browse files
authored
Add FedRAMP example (#1000)
1 parent 5ac3b7b commit a6a39f3

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,7 @@ and copying the internal representation into it.
289289

290290
See also the command-line tool https://github.com/openshift-online/ocm-cli built
291291
on top of this SDK.
292+
293+
## FedRAMP
294+
295+
The OCM SDK fully supports the OCM FedRAMP environment. Additional `TokenURL`, `URL`, and `Client` configuration is required in order to make the connection. An example implementation for the OCM FedRAMP environment can be found in the [examples](examples/fedramp_auth.go) directory.

authentication/transport_wrapper.go

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ const (
4848
DefaultTokenURL = "https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token"
4949
DefaultClientID = "cloud-services"
5050
DefaultClientSecret = ""
51+
52+
FedRAMPTokenURL = "https://sso.openshiftusgov.com/realms/redhat-external/protocol/openid-connect/token"
53+
FedRAMPClientID = "console-dot"
5154
)
5255

5356
// DefaultScopes is the ser of scopes used by default:

connection.go

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const (
5757
DefaultClientSecret = authentication.DefaultClientSecret
5858
DefaultURL = "https://api.openshift.com"
5959
DefaultAgent = "OCM-SDK/" + Version
60+
FedRAMPURL = "https://api.openshiftusgov.com"
6061
)
6162

6263
// DefaultScopes is the ser of scopes used by default:

examples/fedramp_auth.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Copyright (c) 2024 Red Hat, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// This example shows how create a connection using the OpenID refresh token grant for
18+
// FedRAMP authentication.
19+
20+
package main
21+
22+
import (
23+
"context"
24+
"fmt"
25+
"os"
26+
27+
sdk "github.com/openshift-online/ocm-sdk-go"
28+
"github.com/openshift-online/ocm-sdk-go/authentication"
29+
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
30+
"github.com/openshift-online/ocm-sdk-go/logging"
31+
)
32+
33+
func main() {
34+
// Create a context:
35+
ctx := context.Background()
36+
37+
// Create a logger that has the debug level enabled:
38+
logger, err := logging.NewGoLoggerBuilder().
39+
Debug(true).
40+
Build()
41+
if err != nil {
42+
fmt.Fprintf(os.Stderr, "Can't build logger: %v\n", err)
43+
os.Exit(1)
44+
}
45+
46+
// Create the connection, and remember to close it:
47+
connection, err := sdk.NewConnectionBuilder().
48+
Logger(logger).
49+
URL(sdk.FedRAMPURL).
50+
TokenURL(authentication.FedRAMPTokenURL).
51+
Tokens(os.Getenv("OCM_REFRESH_TOKEN")).
52+
Client(authentication.FedRAMPClientID, "").
53+
BuildContext(ctx)
54+
if err != nil {
55+
fmt.Fprintf(os.Stderr, "Can't build connection: %v\n", err)
56+
os.Exit(1)
57+
}
58+
defer connection.Close()
59+
60+
// Get the client for the service that manages the collection of clusters:
61+
collection := connection.ClustersMgmt().V1().Clusters()
62+
63+
// Retrieve the collection of clusters:
64+
response, err := collection.List().
65+
Search("name like 'my%'").
66+
Page(1).
67+
Size(10).
68+
SendContext(ctx)
69+
if err != nil {
70+
fmt.Fprintf(os.Stderr, "Can't retrieve clusters: %v\n", err)
71+
os.Exit(1)
72+
}
73+
74+
// Print the result:
75+
response.Items().Each(func(cluster *cmv1.Cluster) bool {
76+
fmt.Printf("%s - %s\n", cluster.ID(), cluster.Name())
77+
return true
78+
})
79+
}

0 commit comments

Comments
 (0)