View Javadoc

1   package com.atlassian.asap.core.server;
2   
3   import org.apache.commons.lang3.StringUtils;
4   import org.slf4j.Logger;
5   import org.slf4j.LoggerFactory;
6   
7   import java.util.Set;
8   import java.util.regex.Pattern;
9   import java.util.stream.Collectors;
10  
11  import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
12  
13  /**
14   * A command line interface for {@link SimpleServer}.
15   */
16  public class SimpleServerRunner {
17      /**
18       * Port to listen on.
19       */
20      public static final String PORT_SYSPROP = "asap.port";
21  
22      /**
23       * System property that specifies the base URL of the public key server repository to use.
24       */
25      public static final String PUBLIC_KEY_SERVER_URL_SYSPROP = "asap.public.key.repo.url";
26  
27      /**
28       * System property that specifies the resource server audience.
29       */
30      public static final String RESOURCE_SERVER_AUDIENCE_SYSPROP = "asap.resource.server.audience";
31  
32      /**
33       * System property that specifies the authorized subjects for the resource.
34       * The authorized subjects must be a comma separated string of subject names without any whitespaces
35       */
36      public static final String AUTHORIZED_SUBJECT_SYSPROP = "asap.resource.server.authorized.subjects";
37  
38      /**
39       * System property that specifies the authorized issuers for the resource.
40       * The authorized issuers must be a comma separated string of issuer names without any whitespaces.
41       * If omitted or empty, the authorized issuers are equal to the authorized subjects.
42       */
43      public static final String AUTHORIZED_ISSUER_SYSPROP = "asap.resource.server.authorized.issuer";
44  
45      private static String DEFAULT_PUBLIC_KEY_REPO = "classpath://publickeyrepo/";
46  
47      private static final Logger logger = LoggerFactory.getLogger(SimpleServerRunner.class);
48  
49      /**
50       * Main function to run the server.
51       *
52       * @param args command line arguments
53       * @throws Exception if the server fails to start
54       */
55      public static void main(String[] args) throws Exception {
56          int port = Integer.getInteger(PORT_SYSPROP, 8080);
57  
58          String publicKeyBaseUrl = defaultIfBlank(System.getProperty(PUBLIC_KEY_SERVER_URL_SYSPROP), DEFAULT_PUBLIC_KEY_REPO);
59          String audience = defaultIfBlank(System.getProperty(RESOURCE_SERVER_AUDIENCE_SYSPROP), "test-resource-server");
60          String authorizedSubjectsString = defaultIfBlank(System.getProperty(AUTHORIZED_SUBJECT_SYSPROP), "issuer1,issuer2,issuer3");
61          String authorizedIssuersString = defaultIfBlank(System.getProperty(AUTHORIZED_ISSUER_SYSPROP), authorizedSubjectsString);
62          Set<String> authorizedSubjects = Pattern.compile(",").splitAsStream(authorizedSubjectsString).filter(StringUtils::isNotEmpty).collect(Collectors.toSet());
63          Set<String> authorizedIssuers = Pattern.compile(",").splitAsStream(authorizedIssuersString).filter(StringUtils::isNotEmpty).collect(Collectors.toSet());
64  
65          SimpleServer simpleServer = new SimpleServer(port, publicKeyBaseUrl, audience, authorizedSubjects, authorizedIssuers);
66  
67          logger.info("Server initialized with: Audience: {}, pkPath: {}, authorized subjects: {}, authorized issuers: {}",
68                  audience, publicKeyBaseUrl, authorizedSubjects, authorizedIssuers);
69  
70          simpleServer.start();
71      }
72  }