3333import eu .openanalytics .containerproxy .model .runtime .Container ;
3434import eu .openanalytics .containerproxy .model .runtime .Proxy ;
3535import eu .openanalytics .containerproxy .model .runtime .ProxyStatus ;
36+ import eu .openanalytics .containerproxy .model .runtime .runtimevalues .CreatedTimestampKey ;
37+ import eu .openanalytics .containerproxy .model .runtime .runtimevalues .InstanceIdKey ;
38+ import eu .openanalytics .containerproxy .model .runtime .runtimevalues .ProxiedAppKey ;
39+ import eu .openanalytics .containerproxy .model .runtime .runtimevalues .ProxyIdKey ;
40+ import eu .openanalytics .containerproxy .model .runtime .runtimevalues .ProxySpecIdKey ;
41+ import eu .openanalytics .containerproxy .model .runtime .runtimevalues .RealmIdKey ;
42+ import eu .openanalytics .containerproxy .model .runtime .runtimevalues .RuntimeValue ;
43+ import eu .openanalytics .containerproxy .model .runtime .runtimevalues .UserGroupsKey ;
44+ import eu .openanalytics .containerproxy .model .runtime .runtimevalues .UserIdKey ;
3645import eu .openanalytics .containerproxy .model .spec .ContainerSpec ;
3746import eu .openanalytics .containerproxy .service .UserService ;
3847import eu .openanalytics .containerproxy .spec .expression .ExpressionAwareContainerSpec ;
5766import java .util .function .BiConsumer ;
5867import java .util .regex .Matcher ;
5968import java .util .regex .Pattern ;
60- import java .util .stream .Collectors ;
6169
6270public abstract class AbstractContainerBackend implements IContainerBackend {
6371
@@ -69,20 +77,6 @@ public abstract class AbstractContainerBackend implements IContainerBackend {
6977
7078 protected static final String DEFAULT_TARGET_PROTOCOL = "http" ;
7179
72- //TODO rename vars?
73- protected static final String ENV_VAR_USER_NAME = "SHINYPROXY_USERNAME" ;
74- protected static final String ENV_VAR_USER_GROUPS = "SHINYPROXY_USERGROUPS" ;
75- protected static final String ENV_VAR_REALM_ID = "SHINYPROXY_REALM_ID" ;
76-
77- protected static final String RUNTIME_LABEL_PROXY_ID = "openanalytics.eu/sp-proxy-id" ;
78- protected static final String RUNTIME_LABEL_USER_ID = "openanalytics.eu/sp-user-id" ;
79- protected static final String RUNTIME_LABEL_USER_GROUPS = "openanalytics.eu/sp-user-groups" ;
80- protected static final String RUNTIME_LABEL_REALM_ID = "openanalytics.eu/sp-realm-id" ;
81- protected static final String RUNTIME_LABEL_PROXY_SPEC_ID = "openanalytics.eu/sp-spec-id" ;
82- protected static final String RUNTIME_LABEL_CREATED_TIMESTAMP = "openanalytics.eu/sp-proxy-created-timestamp" ;
83- protected static final String RUNTIME_LABEL_PROXIED_APP = "openanalytics.eu/sp-proxied-app" ;
84- protected static final String RUNTIME_LABEL_INSTANCE = "openanalytics.eu/sp-instance" ;
85-
8680 protected final Logger log = LogManager .getLogger (getClass ());
8781
8882 private boolean useInternalNetwork ;
@@ -131,6 +125,7 @@ public SuccessOrFailure<Proxy> startProxy(Proxy proxy) throws ContainerProxyExce
131125 proxy .setId (UUID .randomUUID ().toString ());
132126 proxy .setStatus (ProxyStatus .Starting );
133127 proxy .setCreatedTimestamp (System .currentTimeMillis ());
128+ setRuntimeValues (proxy );
134129
135130 try {
136131 doStartProxy (proxy );
@@ -161,20 +156,6 @@ protected void doStartProxy(Proxy proxy) throws Exception {
161156 for (ContainerSpec spec : proxy .getSpec ().getContainerSpecs ()) {
162157 if (authBackend != null ) authBackend .customizeContainer (spec );
163158
164- // add labels need for App Recovery and maintenance
165- spec .addRuntimeLabel (RUNTIME_LABEL_PROXIED_APP , true , "true" );
166- spec .addRuntimeLabel (RUNTIME_LABEL_INSTANCE , true , instanceId );
167-
168- spec .addRuntimeLabel (RUNTIME_LABEL_PROXY_ID , false , proxy .getId ());
169- spec .addRuntimeLabel (RUNTIME_LABEL_PROXY_SPEC_ID , false , proxy .getSpec ().getId ());
170- if (realmId != null ) {
171- spec .addRuntimeLabel (RUNTIME_LABEL_REALM_ID , false , realmId );
172- }
173- spec .addRuntimeLabel (RUNTIME_LABEL_USER_ID , false , proxy .getUserId ());
174- String [] groups = userService .getGroups (userService .getCurrentAuth ());
175- spec .addRuntimeLabel (RUNTIME_LABEL_USER_GROUPS , false , String .join ("," , groups ));
176- spec .addRuntimeLabel (RUNTIME_LABEL_CREATED_TIMESTAMP , false , String .valueOf (proxy .getCreatedTimestamp ()));
177-
178159 ExpressionAwareContainerSpec eSpec = new ExpressionAwareContainerSpec (spec ,
179160 proxy ,
180161 expressionResolver ,
@@ -238,35 +219,31 @@ protected Long memoryToBytes(String memory) {
238219 }
239220 return mem ;
240221 }
241-
242- protected List <String > buildEnv (ContainerSpec containerSpec , Proxy proxy ) throws IOException {
243- List <String > env = new ArrayList <>();
244- env .add (String .format ("%s=%s" , ENV_VAR_USER_NAME , proxy .getUserId ()));
245-
246- String [] groups = userService .getGroups (userService .getCurrentAuth ());
247- env .add (String .format ("%s=%s" , ENV_VAR_USER_GROUPS , Arrays .stream (groups ).collect (Collectors .joining ("," ))));
248-
249- String realmId = environment .getProperty ("proxy.realm-id" );
250- if (realmId != null ) {
251- env .add (String .format ("%s=%s" , ENV_VAR_REALM_ID , realmId ));
222+
223+ protected Map <String , String > buildEnv (ContainerSpec containerSpec , Proxy proxy ) throws IOException {
224+ Map <String , String > env = new HashMap <>();
225+
226+ for (RuntimeValue runtimeValue : proxy .getRuntimeValues ().values ()) {
227+ if (runtimeValue .getKey ().getIncludeAsEnvironmentVariable ()) {
228+ env .put (runtimeValue .getKey ().getKeyAsEnvVar (), runtimeValue .getValue ());
229+ }
252230 }
253231
254232 String envFile = containerSpec .getEnvFile ();
255233 if (envFile != null && Files .isRegularFile (Paths .get (envFile ))) {
256234 Properties envProps = new Properties ();
257235 envProps .load (new FileInputStream (envFile ));
258236 for (Object key : envProps .keySet ()) {
259- env .add ( String . format ( "%s=%s" , key , envProps .get (key )));
237+ env .put ( key . toString (), envProps .get (key ). toString ( ));
260238 }
261239 }
262240
263241 if (containerSpec .getEnv () != null ) {
264242 for (Map .Entry <String , String > entry : containerSpec .getEnv ().entrySet ()) {
265- env .add ( String . format ( "%s=%s" , entry .getKey (), entry .getValue () ));
243+ env .put ( entry .getKey (), entry .getValue ());
266244 }
267245 }
268246
269- // Allow the authentication backend to add values to the environment, if needed.
270247 if (authBackend != null ) authBackend .customizeContainerEnv (env );
271248
272249 return env ;
@@ -352,4 +329,21 @@ public static String computeTargetPath(String targetPath) {
352329
353330 return targetPath ;
354331 }
332+
333+ private void setRuntimeValues (Proxy proxy ) {
334+ proxy .addRuntimeValue (new RuntimeValue (ProxiedAppKey .inst , "true" ));
335+ proxy .addRuntimeValue (new RuntimeValue (ProxyIdKey .inst , proxy .getId ()));
336+ proxy .addRuntimeValue (new RuntimeValue (InstanceIdKey .inst , instanceId ));
337+ proxy .addRuntimeValue (new RuntimeValue (ProxySpecIdKey .inst , proxy .getSpec ().getId ()));
338+
339+ if (realmId != null ) {
340+ proxy .addRuntimeValue (new RuntimeValue (RealmIdKey .inst , realmId ));
341+ }
342+
343+ proxy .addRuntimeValue (new RuntimeValue (UserIdKey .inst , proxy .getUserId ()));
344+ String [] groups = userService .getGroups (userService .getCurrentAuth ());
345+ proxy .addRuntimeValue (new RuntimeValue (UserGroupsKey .inst , String .join ("," , groups )));
346+ proxy .addRuntimeValue (new RuntimeValue (CreatedTimestampKey .inst , Long .toString (proxy .getCreatedTimestamp ())));
347+ }
348+
355349}
0 commit comments