Skip to content

Commit 99b61f6

Browse files
Merge pull request #275 from clcollins/check_unmarshal_error
Fix Jira token parsing and handles errors
2 parents 42227cd + bd63a19 commit 99b61f6

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

pkg/featureSet/jira/jira.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88

99
"github.com/openshift/ocm-container/pkg/engine"
10+
log "github.com/sirupsen/logrus"
1011
)
1112

1213
const (
@@ -18,7 +19,7 @@ const (
1819
)
1920

2021
type Config struct {
21-
Token string
22+
Token string `json:"token"`
2223
Env map[string]string
2324
Mounts []engine.VolumeMount
2425
}
@@ -48,26 +49,36 @@ func New(home string, rw bool) (*Config, error) {
4849

4950
// Else we need to read the token from the file
5051
t := home + "/" + jiraTokenFile
52+
log.Debug(fmt.Sprintf("Jira token ('%v') or authType ('%v') not found in env, reading from file: %v", token, authType, t))
5153
_, err = os.Stat(t)
5254
if err != nil {
53-
return config, fmt.Errorf("error: problem reading Jira token file: %v: %v, err", t, err)
55+
return config, fmt.Errorf("error: problem reading Jira token file: %v: %v", t, err)
5456
}
5557

5658
f, err := os.Open(t)
5759
if err != nil {
58-
return config, fmt.Errorf("error: problem reading Jira token file: %v: %v, err", t, err)
60+
return config, fmt.Errorf("error: problem reading Jira token file: %v: %v", t, err)
5961
}
6062
defer f.Close()
6163

6264
b, err := io.ReadAll(f)
6365
if err != nil {
64-
return config, fmt.Errorf("error: problem reading Jira token file: %v: %v, err", t, err)
66+
return config, fmt.Errorf("error: problem reading Jira token file: %v: %v", t, err)
6567
}
6668

67-
json.Unmarshal(b, &token)
68-
config.Env[jiraTokenEnv] = token
69+
err = json.Unmarshal(b, &config)
70+
if err != nil {
71+
return config, fmt.Errorf("error: problem reading Jira token file: %v: %v", t, err)
72+
}
73+
config.Env[jiraTokenEnv] = config.Token
6974
config.Env[jiraAuthTypeEnv] = "bearer"
7075

76+
log.Debug(fmt.Sprintf("Using JiraConfig: %v", config))
77+
78+
if config.Env[jiraTokenEnv] == "" {
79+
return config, fmt.Errorf("error: Jira token not found in env or file: %v", t)
80+
}
81+
7182
return config, nil
7283
}
7384

pkg/ocmcontainer/ocmcontainer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func New(cmd *cobra.Command, args []string) (*ocmContainer, error) {
170170
if err != nil {
171171
return o, err
172172
}
173-
maps.Copy(jiraConfig.Env, c.Envs)
173+
maps.Copy(c.Envs, jiraConfig.Env)
174174
c.Volumes = append(c.Volumes, jiraConfig.Mounts...)
175175
}
176176

0 commit comments

Comments
 (0)