25
25
from cliff import help
26
26
27
27
from orpy .client import client
28
+ from orpy import oidc
28
29
from orpy import utils
29
30
from orpy import version
30
31
@@ -42,6 +43,7 @@ def __init__(self):
42
43
43
44
self .client = None
44
45
self .token = None
46
+ self .oidc_agent = None
45
47
46
48
# Patch command.Command to add a default auth_required = True
47
49
command .Command .auth_required = True
@@ -62,9 +64,17 @@ def initialize_app(self, argv):
62
64
for cmd in self .commands :
63
65
self .command_manager .add_command (cmd .__name__ .lower (), cmd )
64
66
self .token = utils .env ("ORCHESTRATOR_TOKEN" )
67
+
68
+ if self .options .oidc_agent_sock and self .options .oidc_agent_account :
69
+ self .oidc_agent = oidc .OpenIDConnectAgent (
70
+ self .options .oidc_agent_account ,
71
+ socket_path = self .options .oidc_agent_sock
72
+ )
73
+
65
74
if self .client is None :
66
75
self .client = client .OrpyClient (self .options .orchestrator_url ,
67
- self .token ,
76
+ oidc_agent = self .oidc_agent ,
77
+ token = self .token ,
68
78
debug = self .options .debug )
69
79
70
80
def prepare_to_run_command (self , cmd ):
@@ -76,21 +86,72 @@ def prepare_to_run_command(self, cmd):
76
86
"use --url or set the ORCHESTRATOR_URL "
77
87
"environment variable." )
78
88
79
- if cmd .auth_required and not self .token :
80
- self .parser .error ("No token has been provided, please set the "
81
- "ORCHESTRATOR_TOKEN environment variable "
82
- "(see '%s help' for more details on how "
83
- "to set up authentication)" % self .parser .prog )
89
+ if cmd .auth_required :
90
+ if (not all ([self .options .oidc_agent_sock ,
91
+ self .options .oidc_agent_account ])) and not self .token :
92
+
93
+ self .parser .error ("No oidc-agent has been set up or no access "
94
+ "token has been provided, please set the "
95
+ "ORCHESTRATOR_TOKEN environment variable or "
96
+ "set up an oidc-agent "
97
+ "(see '%s help' for more details on how "
98
+ "to set up authentication)" %
99
+ self .parser .prog )
84
100
85
101
def build_option_parser (self , description , version ):
102
+ auth_help = """Authentication:
103
+
104
+ In order to interact with the INDIGO PaaS Orchestrator we need to use an
105
+ OpenID Connect access token from a trusted OpenID Connect provider at the
106
+ orchestrator.
107
+
108
+ Please either store your access token in 'ORCHESTRATOR_TOKEN' or set the
109
+ account to use with oidc-agent in the 'OIDC_ACCOUNT' and the socket path
110
+ of the oidc-agent in the 'OIDC_SOCK' environment variable:
111
+
112
+ export ORCHESTRATOR_TOKEN=<your access token>
113
+ OR
114
+ export OIDC_SOCK=<path to the oidc-agent socket>
115
+ export OIDC_ACCOUNT=<account to use>
116
+
117
+ Usually, the OIDC_SOCK environmental variable is already exported if you
118
+ are using oidc-agent.
119
+
120
+ As an alternative, you can pass the socket path and the account through
121
+ the command line with the --oidc-agent-sock and --oidc-agent-account
122
+ parameters.
123
+
124
+ """
86
125
parser = super (OrpyApp , self ).build_option_parser (
87
126
self .__doc__ ,
88
127
version ,
89
128
argparse_kwargs = {
90
- "formatter_class" : argparse .RawDescriptionHelpFormatter
129
+ "formatter_class" : argparse .RawDescriptionHelpFormatter ,
130
+ "epilog" : auth_help ,
91
131
})
92
132
93
- # service token auth argument
133
+ parser .add_argument (
134
+ '--oidc-agent-sock' ,
135
+ metavar = '<oidc-agent-socket>' ,
136
+ dest = 'oidc_agent_sock' ,
137
+ default = utils .env ('OIDC_SOCK' ),
138
+ help = 'The path for the oidc-agent socket to use to get and renew '
139
+ 'access tokens from the OpenID Connect provider. This '
140
+ 'defaults to the OIDC_SOCK environment variable, that should '
141
+ 'be automatically set up if you are using oidc-agent. '
142
+ 'In order to use the oidc-agent you must also pass the '
143
+ '--oidc-agent-account parameter, or set the OIDC_ACCOUNT '
144
+ 'environment variable.'
145
+ )
146
+ parser .add_argument (
147
+ '--oidc-agent-account' ,
148
+ metavar = '<oidc-agent-account>' ,
149
+ dest = 'oidc_agent_account' ,
150
+ default = utils .env ('OIDC_ACCOUNT' ),
151
+ help = 'The oidc-agent account that we will use to get tokens from. '
152
+ 'In order to use the oidc-agent you must pass thos parameter '
153
+ 'or set the OIDC_ACCOUNT environment variable.'
154
+ )
94
155
parser .add_argument (
95
156
'--url' ,
96
157
metavar = '<orchestrator-url>' ,
0 commit comments