Skip to content

Commit d0d8f54

Browse files
Merge pull request #4838 from dkrupp/password_env
Enable passing credentials from env var
2 parents fcf4c7a + 695870e commit d0d8f54

5 files changed

Lines changed: 88 additions & 29 deletions

File tree

docs/web/authentication.md

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@ the results stored on a server.
99
1010
Table of Contents
1111
=================
12-
* [Server-side configuration](#server-side-configuration)
13-
* [<i>Dictionary</i> authentication](#dictionary-authentication)
14-
* [External authentication methods](#external-auth-methods)
15-
* [<i>PAM</i> authentication](#pam-authentication)
16-
* [<i>LDAP</i> authentication](#ldap-authentication)
17-
* [Configuration options](#configuration-options)
18-
* [Membership in custom groups with <i>regex_groups</i>](#regex_groups-authentication)
19-
* [<i>OAuth</i> authentication](#oauth-authentication)
20-
* [<i>OAuth</i> Configuration options](#oauth-configuration-options)
21-
* [<i>OAuth</i> details per each provider](#oauth-details-per-each-provider)
22-
* [Client-side configuration](#client-side-configuration)
23-
* [Web-browser client](#web-browser-client)
24-
* [Command-line client](#command-line-client)
25-
* [Preconfigured credentials](#preconfigured-credentials)
26-
* [Automatic login](#automatic-login)
27-
* [Currently active tokens](#currently-active-tokens)
28-
* [Personal access token](#personal-access-token)
29-
* [`new`](#new-personal-access-token)
30-
* [`list`](#list-personal-access-token)
31-
* [`del`](#remove-personal-access-token)
12+
- [CodeChecker authentication subsystem](#codechecker-authentication-subsystem)
13+
- [Table of Contents](#table-of-contents)
14+
- [Server-side configuration ](#server-side-configuration-)
15+
- [Dictionary authentication ](#dictionary-authentication-)
16+
- [External authentication methods ](#external-authentication-methods-)
17+
- [PAM authentication ](#pam-authentication-)
18+
- [LDAP authentication ](#ldap-authentication-)
19+
- [Configuration options ](#configuration-options-)
20+
- [Membership in custom groups with regex\_groups](#membership-in-custom-groups-with-regex_groups)
21+
- [OAuth authentication ](#oauth-authentication-)
22+
- [OAuth Configuration options ](#oauth-configuration-options-)
23+
- [OAuth Details per each provider ](#oauth-details-per-each-provider-)
24+
- [Client-side configuration ](#client-side-configuration-)
25+
- [Web-browser client ](#web-browser-client-)
26+
- [Command-line client ](#command-line-client-)
27+
- [Preconfigured credentials ](#preconfigured-credentials-)
28+
- [Automatic login ](#automatic-login-)
29+
- [Currently active tokens ](#currently-active-tokens-)
30+
- [Personal access token ](#personal-access-token-)
31+
- [New personal access token ](#new-personal-access-token-)
32+
- [List personal access tokens ](#list-personal-access-tokens-)
33+
- [Remove personal access token ](#remove-personal-access-token-)
3234

3335
# Server-side configuration <a name="server-side-configuration"></a>
3436

@@ -360,7 +362,7 @@ Specific behavior related to each provider is configured by a provider `template
360362
A key-value table that is used to set variables across all providers, for convenience.
361363

362364
Any variable can be specified. If using the `host` variable, it should be in the format `https://example.com`, including the protocol.
363-
365+
364366
The `callback_url`'s default value uses the `host` and `provider` variables.
365367
Template `ms_entra/v2.0` uses the `tenant_id` variable.
366368

@@ -394,7 +396,7 @@ Specific behavior related to each provider is configured by a provider `template
394396

395397
* `variables`
396398

397-
A key-value table that is used to set variables used inside parameters.
399+
A key-value table that is used to set variables used inside parameters.
398400
To use a variable, specify it using `{variable}`.
399401
The `{provider}` variable is automatically set.
400402

@@ -494,7 +496,7 @@ Specific behavior related to each provider is configured by a provider `template
494496
*Default*: Set by template.
495497

496498
### 🔧 Example: OAuth Configuration using templates
497-
499+
498500
```jsonc
499501
"github": {
500502
"enabled": false,
@@ -622,7 +624,14 @@ To alleviate the need for supplying authentication in the command-line every
622624
time a server is connected to, users can pre-configure their credentials to be
623625
used in authentication.
624626
625-
To do so first copy the `config/session_client.json` file from the CodeChecker
627+
You can pass the credential used as a password or personal access token
628+
in the `.codechecker.password.json` or
629+
directly in the `CC_PASSWORD` environment variable.
630+
If this environment variable is set, the credentials
631+
described in `.codechecker.password.json` will be ignored.
632+
633+
If you wish to provide (multiple different) credentials in a password configuration file,
634+
first copy the `config/session_client.json` file from the CodeChecker
626635
package to your home directory and rename it to `.codechecker.passwords.json`
627636
After creating the new file open `~/.codechecker.passwords.json`.
628637
@@ -656,10 +665,9 @@ authenticate in the name of the given user. This way no need to store passwords
656665
in text files. For more information [see](#personal-access-token).
657666

658667
The location of the password file can be configured by the `CC_PASS_FILE`
659-
environment variable. This environment variable can also be used to setup
660-
different credential files to login to the same server with a different user.
668+
environment variable.
661669

662-
Furthermore, the location of the session file can be configured by the
670+
The location of the session file can be configured by the
663671
`CC_SESSION_FILE` environment variable. This can be useful if CodeChecker does
664672
not have the permission to create a session file under the user's home
665673
directory (e.g. in some CI environments).

web/client/codechecker_client/cli/store.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ def get_argparser_ctor_args():
165165
'epilog': """
166166
Environment variables
167167
------------------------------------------------
168+
CC_PASSWORD You can provide authentication credentials directly in this
169+
variable. "CodeChecker cmd login" will use it as a
170+
password or personal access token if set.
171+
168172
CC_PASS_FILE The location of the password file for auto login. By default
169173
CodeChecker will use '~/.codechecker.passwords.json' file.
170174
It can also be used to setup different credential files to

web/client/codechecker_client/client.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,19 @@ def login_user(protocol, host, port, username, login=False):
111111
methods = auth_client.getAcceptedAuthMethods()
112112
# Attempt username-password auth first.
113113
if 'Username:Password' in str(methods):
114-
115114
# Try to use a previously saved credential from configuration file if
116115
# autologin is enabled.
117116
saved_auth = None
118-
if session.is_autologin_enabled():
117+
if env.get_password_string():
118+
# if CC_PASSWORD env var is defined use that
119+
LOG.info("Using credentials from CC_PASSWORD env var to log in.")
120+
if not username:
121+
LOG.error("Cannot login. CC_PASSWORD env var is defined,"
122+
"but username is not provided!")
123+
sys.exit(1)
124+
saved_auth = f"{username}:{env.get_password_string()}"
125+
126+
if session.is_autologin_enabled() and not saved_auth:
119127
saved_auth = session.get_auth_string(host, port)
120128

121129
if saved_auth:

web/codechecker_web/shared/env.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ def get_default_workspace():
2626
return workspace
2727

2828

29+
def get_password_string():
30+
""" Return the location of the CodeChecker password credential. """
31+
return os.environ.get("CC_PASSWORD", None)
32+
33+
2934
def get_password_file():
3035
""" Return the location of the CodeChecker password file. """
3136
return os.environ.get("CC_PASS_FILE",

web/tests/functional/authentication/test_authentication.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,40 @@ def test_nonauth_storage(self):
542542
subprocess.check_output(
543543
store_cmd, encoding="utf-8", errors="ignore")
544544

545+
def test_cc_password_env_auth(self):
546+
"""
547+
Tests authentication if credential is passed
548+
in the CC_PASSWORD env var.
549+
"""
550+
551+
codechecker_cfg = self._test_cfg['codechecker_cfg']
552+
553+
# Login with good password
554+
my_env = os.environ.copy()
555+
my_env["CC_PASSWORD"] = "test"
556+
557+
login_cmd = [env.codechecker_cmd(), 'cmd', 'login', 'cc',
558+
'--url', env.parts_to_url(codechecker_cfg)]
559+
560+
login_response = subprocess.check_output(
561+
login_cmd, env=my_env, encoding="utf-8", errors="ignore")
562+
self.assertTrue(login_response,
563+
"Server reported successful authentication")
564+
565+
logout_cmd = [env.codechecker_cmd(), 'cmd', 'login', 'cc',
566+
'-d', '--url', env.parts_to_url(codechecker_cfg)]
567+
subprocess.check_output(
568+
logout_cmd, env=my_env, encoding="utf-8", errors="ignore")
569+
# Login with wrong password
570+
my_env["CC_PASSWORD"] = "wrong"
571+
572+
login_cmd = [env.codechecker_cmd(), 'cmd', 'login', 'cc',
573+
'--url', env.parts_to_url(codechecker_cfg)]
574+
575+
with self.assertRaises(subprocess.CalledProcessError):
576+
subprocess.check_output(
577+
login_cmd, env=my_env, encoding="utf-8", errors="ignore")
578+
545579
def test_group_auth(self):
546580
"""
547581
Test for case insensitive group comparison at authorization.

0 commit comments

Comments
 (0)