Skip to content

Commit

Permalink
lobster-codebeamer supports token authentication
Browse files Browse the repository at this point in the history
One new argument to pass the token has been added to lobster-codebeamer. The user can also define the token in cb config file.

Resolves #69
  • Loading branch information
TannazVhdBMWExt committed Aug 21, 2024
1 parent 0aa5553 commit 5fdf394
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

### 0.9.18-dev

* The `lobster-codebeamer` tool now uses an authentication token.
Token can be added either in the config file or as an argument.

* The `lobster-codebeamer` tool now supports `refs` as an upstream reference

* The `lobster-online-report` tool now works with config files located in
Expand Down
41 changes: 30 additions & 11 deletions lobster/tools/codebeamer/codebeamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@
from lobster.errors import Message_Handler, LOBSTER_Error
from lobster.io import lobster_read, lobster_write

TOKEN = 'token'

class References(Enum):
REFS = "refs"


SUPPORTED_REFERENCES = [References.REFS.value]


def add_refs_refrences(req, flat_values_list):
# refs
for value in flat_values_list:
Expand All @@ -70,15 +69,26 @@ def add_refs_refrences(req, flat_values_list):
References.REFS.value: add_refs_refrences
}

class BearerAuth(requests.auth.AuthBase):
def __init__(self, token):
self.token = token
def __call__(self, r):
r.headers['Authorization'] = f'Bearer {self.token}'
return r

def query_cb_single(cb_config, url):
assert isinstance(cb_config, dict)
assert isinstance(url, str)

try:
if cb_config["token"]:
auth = BearerAuth(cb_config["token"])
else:
auth = (cb_config["user"],
cb_config["pass"])

result = requests.get(url,
auth=(cb_config["user"],
cb_config["pass"]),
auth=auth,
timeout=cb_config["timeout"],
verify=cb_config["verify_ssl"])
except requests.exceptions.ReadTimeout:
Expand Down Expand Up @@ -272,6 +282,12 @@ def parse_cb_config(file_name):
with open(file_name, "r", encoding='utf-8') as file:
data = json.loads(file.read())

token = None
json_config = {}

if TOKEN in data:
token = data.pop(TOKEN)

provided_config_keys = set(data.keys())
supported_references = set(SUPPORTED_REFERENCES)

Expand All @@ -280,10 +296,9 @@ def parse_cb_config(file_name):
"supported referenes: '%s'" %
', '.join(SUPPORTED_REFERENCES))

json_config = {}
for key, value in data.items():
json_config[key] = ensure_array_of_strings(value)
return json_config
return json_config, token


def main():
Expand Down Expand Up @@ -322,6 +337,7 @@ def main():
ap.add_argument("--cb-root", default=os.environ.get("CB_ROOT", None))
ap.add_argument("--cb-user", default=os.environ.get("CB_USERNAME", None))
ap.add_argument("--cb-pass", default=os.environ.get("CB_PASSWORD", None))
ap.add_argument("--cb-token", default=None)
ap.add_argument("--out", default=None)
options = ap.parse_args()

Expand All @@ -332,14 +348,17 @@ def main():
"base" : "%s/cb/api/v3" % options.cb_root,
"user" : options.cb_user,
"pass" : options.cb_pass,
"token" : options.cb_token,
"verify_ssl" : not options.ignore_ssl_errors,
"page_size" : options.query_size,
"timeout" : options.timeout,
}

if options.config:
if os.path.isfile(options.config):
cb_config["references"] = parse_cb_config(options.config)
cb_config["references"], config_token = parse_cb_config(options.config)
if not cb_config["token"] and config_token:
cb_config["token"] = config_token
else:
ap.error("cannot open config file '%s'" % options.config)

Expand All @@ -359,10 +378,10 @@ def main():
print("using .netrc login for %s" % cb_config["root"])
cb_config["user"], _, cb_config["pass"] = auth

if cb_config["user"] is None:
ap.error("please set CB_USERNAME or use --cb-user")
if cb_config["pass"] is None:
ap.error("please set CB_PASSWORD or use --cb-pass")
if (cb_config["token"] is None and
(cb_config["user"] is None or cb_config["pass"] is None)):
ap.error("please set --cb-token or add your token to the config-file"
"or use --cb-user and --cb-pass")

items_to_import = set()

Expand Down
8 changes: 8 additions & 0 deletions packages/lobster-tool-codebeamer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ or
}
```

Is it also possible to define the codebeamer token in this file:
```
{
"refs" : "cb-fieldname",
"token" : "your cb-token"
}
```

## Usage

There are two ways you can use this tool:
Expand Down

0 comments on commit 5fdf394

Please sign in to comment.