Skip to content

Commit 986040f

Browse files
committed
QC-1289 Read BKP client token from a file
This adds a possibility to read a BKP client token from a file `qc_bkp_client_token` provided in the working directory if it was not found as environment variable.
1 parent 81883c5 commit 986040f

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

Framework/src/Bookkeeping.cxx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "BookkeepingApi/BkpClientFactory.h"
2121
#include "BookkeepingApi/BkpClient.h"
2222
#include <unistd.h>
23+
#include <filesystem>
24+
#include <fstream>
2325

2426
using namespace o2::bkp::api;
2527

@@ -28,12 +30,33 @@ namespace o2::quality_control::core
2830

2931
std::string readClientToken()
3032
{
33+
// first we try to find the token in the environment variable
3134
if (auto tokenEnv = std::getenv("QC_BKP_CLIENT_TOKEN"); tokenEnv != NULL && std::strlen(tokenEnv) > 0) {
3235
ILOG(Info, Ops) << "Using token from environment variable QC_BKP_CLIENT_TOKEN" << ENDM;
3336
return tokenEnv;
3437
}
3538

36-
ILOG(Debug, Devel) << "Could not find an env var QC_BKP_CLIENT_TOKEN, using BKP client without an authentication token" << ENDM;
39+
// if the environment variable is not set, we try to read it from a file
40+
const std::string tokenFileName = "qc_bkp_client_token.txt";
41+
std::filesystem::path tokenPath = std::filesystem::current_path() / tokenFileName;
42+
43+
std::error_code ec;
44+
if (std::filesystem::exists(tokenPath, ec) && !ec.value()) {
45+
std::string token;
46+
std::ifstream tokenFile(tokenPath);
47+
// from now on, we throw if something goes wrong, because the user is clearly trying to use a token file
48+
if (!tokenFile.is_open()) {
49+
throw std::runtime_error("BKP token file '" + tokenFileName + "' was provided but cannot be opened, check permissions");
50+
}
51+
std::getline(tokenFile, token);
52+
if (token.empty()) {
53+
throw std::runtime_error("BKP token file '" + tokenFileName + "' was provided but it is empty, please provide a valid token");
54+
}
55+
ILOG(Debug, Devel) << "Using token from file qc_bkp_client_token" << ENDM;
56+
return token;
57+
}
58+
59+
ILOG(Debug, Devel) << "Could not find an env var QC_BKP_CLIENT_TOKEN nor a qc_bkp_client_token.txt file, using BKP client without an authentication token" << ENDM;
3760
return "";
3861
}
3962

doc/Framework.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,11 @@ If you need the directory structure preserved, add the argument `--preserve-dire
587587

588588
The framework allows to propagate Quality Objects (QOs) produced by Checks and Aggregators to RCT in Bookkeeping.
589589
The synchronisation is done once, at the end of workflow runtime, i.e. at the End of Run or in the last stage of QC merging on Grid.
590+
Check results are converted into Flags, which are documented in [O2/DataFormats/QualityControl](https://github.com/AliceO2Group/AliceO2/tree/dev/DataFormats/QualityControl).
591+
Information about the object validity is preserved, which allows for time-based flagging of good/bad data.
592+
593+
### Configuration details
594+
590595
Propagation can be enabled by adding the following key-value pair to Check/Aggregator configuration:
591596

592597
```json
@@ -595,10 +600,25 @@ Propagation can be enabled by adding the following key-value pair to Check/Aggre
595600

596601
Using it for Aggregators is discouraged, as the information on which exact Check failed is lost or at least obfuscated.
597602

598-
Also, make sure that the configuration file includes the Bookkeeping URL and there is an env var `QC_BKP_CLIENT_TOKEN` with authentication token for setups external to P2.
603+
To allow QC to connect to Bookkeeping, include the its URL in the QC configuration file, e.g.:
599604

600-
Check results are converted into Flags, which are documented in [O2/DataFormats/QualityControl](https://github.com/AliceO2Group/AliceO2/tree/dev/DataFormats/QualityControl).
601-
Information about the object validity is preserved, which allows for time-based flagging of good/bad data.
605+
```json
606+
{
607+
"qc": {
608+
"config": {
609+
"bookkeeping": {
610+
"url": "bookkeeping.cern.ch:12345"
611+
}
612+
}
613+
}
614+
}
615+
```
616+
617+
For setups external to P2, one also needs to provide a BKP client token.
618+
It can be done by creating a file named `qc_bkp_client_token` in the working directory, containing just the token.
619+
In such case, please ensure minimal permissions for the file, so that it is not readable by other users.
620+
Alternatively, it can be provided as an environment variable `QC_BKP_CLIENT_TOKEN`.
621+
Then, avoid printing the environment variable in the logs.
602622

603623
### Conversion details
604624

0 commit comments

Comments
 (0)