Skip to content

Commit 9a7a4c8

Browse files
committed
Allows passing in the ocm config for short-lived ocm token support
1 parent 2e5ef59 commit 9a7a4c8

File tree

5 files changed

+49
-19
lines changed

5 files changed

+49
-19
lines changed

pkg/ocm/ocm.go

+33
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ package ocm
55
// creating the container
66

77
import (
8+
"os"
9+
"path/filepath"
10+
811
sdk "github.com/openshift-online/ocm-sdk-go"
912
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
1013
"github.com/openshift/osdctl/pkg/utils"
@@ -106,3 +109,33 @@ func GetClusterId(ocmClient *sdk.Connection, key string) (string, error) {
106109

107110
return cluster.ID(), err
108111
}
112+
113+
// Finds the OCM Configuration file and returns the path to it
114+
// Taken wholesale from openshift-online/ocm-cli
115+
func GetOCMConfigLocation() (string, error) {
116+
if ocmconfig := os.Getenv("OCM_CONFIG"); ocmconfig != "" {
117+
return ocmconfig, nil
118+
}
119+
120+
// Determine home directory to use for the legacy file path
121+
home, err := os.UserHomeDir()
122+
if err != nil {
123+
return "", err
124+
}
125+
126+
path := filepath.Join(home, ".ocm.json")
127+
128+
_, err = os.Stat(path)
129+
if os.IsNotExist(err) {
130+
// Determine standard config directory
131+
configDir, err := os.UserConfigDir()
132+
if err != nil {
133+
return path, err
134+
}
135+
136+
// Use standard config directory
137+
path = filepath.Join(configDir, "/ocm/ocm.json")
138+
}
139+
140+
return path, nil
141+
}

pkg/ocmcontainer/ocmcontainer.go

+14
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ func New(cmd *cobra.Command, args []string) (*ocmContainer, error) {
125125
maps.Copy(c.Envs, backplaneConfig.Env)
126126
c.Volumes = append(c.Volumes, backplaneConfig.Mounts...)
127127

128+
// Copy the ocm config into the container
129+
ocmConfigLocation, err := ocm.GetOCMConfigLocation()
130+
if err != nil {
131+
return o, err
132+
}
133+
134+
ocmVolume := engine.VolumeMount{
135+
Source: ocmConfigLocation,
136+
Destination: "/root/.config/ocm/ocm.json",
137+
MountOptions: "ro",
138+
}
139+
140+
c.Volumes = append(c.Volumes, ocmVolume)
141+
128142
ocmConfig, err := ocm.New(viper.GetString("ocm-url"))
129143
if err != nil {
130144
return o, err

utils/bashrc.d/09-ocm.bashrc renamed to utils/bashrc.d/00-ocm.bashrc

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
#!/usr/bin/env bash
22

3-
if [ "x${OFFLINE_ACCESS_TOKEN}" == "x" ]
4-
then
5-
echo "WARNING: must set env variable OFFLINE_ACCESS_TOKEN for automatic OCM login"
6-
return
7-
fi
8-
93
if [ "$OCM_URL" == "" ]
104
then
115
OCM_URL="https://api.openshift.com"
126
fi
137

14-
CLI="${CLI:-ocm}"
15-
if [[ "${CLI}" == "ocm" ]]
8+
if ! ocm whoami &> /dev/null
169
then
17-
LOGIN_ENV='--url'
18-
elif [[ "${CLI}" == "moactl" ]]
19-
then
20-
LOGIN_ENV='--env'
10+
ocm login --url=$OCM_URL --use-device-code
2111
fi
2212

23-
"${CLI}" login --token=$OFFLINE_ACCESS_TOKEN ${LOGIN_ENV}=$OCM_URL
24-
2513
# Wrap the ocm backplane console command to handle automation for
2614
# port mapping inside the container
2715
ocm() {

utils/bin/sre-login

-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ then
1414
exit 1
1515
fi
1616

17-
# Login to OCM first
18-
ocm-login > /dev/null
19-
2017
echo "Logging into cluster $1"
2118

2219
function get_cluster_json {
@@ -62,6 +59,4 @@ cluster_id=$(jq -r '.id' <<< "$clusterjson")
6259
cluster_listening=$(jq -r '.api.listening' <<< "$clusterjson")
6360

6461
# Login to the Cluster
65-
66-
echo "Cluster ID: $cluster_id"
6762
exec ocm backplane login ${cluster_id}

0 commit comments

Comments
 (0)