|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/microcks/microcks-cli/pkg/config" |
| 11 | + "github.com/microcks/microcks-cli/pkg/connectors" |
| 12 | +) |
| 13 | + |
| 14 | +type importURLCommand struct { |
| 15 | +} |
| 16 | + |
| 17 | +func NewImportURLCommand() Command { |
| 18 | + return new(importURLCommand) |
| 19 | +} |
| 20 | + |
| 21 | +func (c *importURLCommand) Execute() { |
| 22 | + // Parse subcommand args first. |
| 23 | + if len(os.Args) < 2 { |
| 24 | + fmt.Println("import-url command require <specificationFile1URL[:primary],specificationFile2URL[:primary]> args") |
| 25 | + os.Exit(1) |
| 26 | + } |
| 27 | + |
| 28 | + specificationFiles := os.Args[2] |
| 29 | + |
| 30 | + // Then parse flags. |
| 31 | + importCmd := flag.NewFlagSet("import-url", flag.ExitOnError) |
| 32 | + |
| 33 | + var microcksURL string |
| 34 | + var keycloakURL string |
| 35 | + var keycloakClientID string |
| 36 | + var keycloakClientSecret string |
| 37 | + var insecureTLS bool |
| 38 | + var caCertPaths string |
| 39 | + var verbose bool |
| 40 | + |
| 41 | + importCmd.StringVar(µcksURL, "microcksURL", "", "Microcks API URL") |
| 42 | + importCmd.StringVar(&keycloakClientID, "keycloakClientId", "", "Keycloak Realm Service Account ClientId") |
| 43 | + importCmd.StringVar(&keycloakClientSecret, "keycloakClientSecret", "", "Keycloak Realm Service Account ClientSecret") |
| 44 | + importCmd.BoolVar(&insecureTLS, "insecure", false, "Whether to accept insecure HTTPS connection") |
| 45 | + importCmd.StringVar(&caCertPaths, "caCerts", "", "Comma separated paths of CRT files to add to Root CAs") |
| 46 | + importCmd.BoolVar(&verbose, "verbose", false, "Produce dumps of HTTP exchanges") |
| 47 | + importCmd.Parse(os.Args[3:]) |
| 48 | + |
| 49 | + // Validate presence and values of flags. |
| 50 | + if len(microcksURL) == 0 { |
| 51 | + fmt.Println("--microcksURL flag is mandatory. Check Usage.") |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | + if len(keycloakClientID) == 0 { |
| 55 | + fmt.Println("--keycloakClientId flag is mandatory. Check Usage.") |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | + if len(keycloakClientSecret) == 0 { |
| 59 | + fmt.Println("--keycloakClientSecret flag is mandatory. Check Usage.") |
| 60 | + os.Exit(1) |
| 61 | + } |
| 62 | + |
| 63 | + // Collect optional HTTPS transport flags. |
| 64 | + if insecureTLS { |
| 65 | + config.InsecureTLS = true |
| 66 | + } |
| 67 | + if len(caCertPaths) > 0 { |
| 68 | + config.CaCertPaths = caCertPaths |
| 69 | + } |
| 70 | + if verbose { |
| 71 | + config.Verbose = true |
| 72 | + } |
| 73 | + |
| 74 | + // Now we seems to be good ... |
| 75 | + // First - retrieve the Keycloak URL from Microcks configuration. |
| 76 | + mc := connectors.NewMicrocksClient(microcksURL) |
| 77 | + keycloakURL, err := mc.GetKeycloakURL() |
| 78 | + if err != nil { |
| 79 | + fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err) |
| 80 | + os.Exit(1) |
| 81 | + } |
| 82 | + |
| 83 | + var oauthToken string = "unauthentifed-token" |
| 84 | + if keycloakURL != "null" { |
| 85 | + // If Keycloak is enabled, retrieve an OAuth token using Keycloak Client. |
| 86 | + kc := connectors.NewKeycloakClient(keycloakURL, keycloakClientID, keycloakClientSecret) |
| 87 | + |
| 88 | + oauthToken, err = kc.ConnectAndGetToken() |
| 89 | + if err != nil { |
| 90 | + fmt.Printf("Got error when invoking Keycloack client: %s", err) |
| 91 | + os.Exit(1) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Then - for each specificationFile, upload the artifact on Microcks Server. |
| 96 | + mc.SetOAuthToken(oauthToken) |
| 97 | + |
| 98 | + sepSpecificationFiles := strings.Split(specificationFiles, ",") |
| 99 | + for _, f := range sepSpecificationFiles { |
| 100 | + mainArtifact := true |
| 101 | + secret := "" |
| 102 | + |
| 103 | + // Check if URL starts with https or http |
| 104 | + if strings.HasPrefix(f, "https://") || strings.HasPrefix(f, "http://") { |
| 105 | + urlAndMainAtrifactAndSecretName := strings.Split(f, ":") |
| 106 | + n := len(urlAndMainAtrifactAndSecretName) |
| 107 | + f = urlAndMainAtrifactAndSecretName[0] + ":" + urlAndMainAtrifactAndSecretName[1] |
| 108 | + if n > 2 { |
| 109 | + val, err := strconv.ParseBool(urlAndMainAtrifactAndSecretName[2]) |
| 110 | + if err != nil { |
| 111 | + fmt.Println(err) |
| 112 | + } |
| 113 | + mainArtifact = val |
| 114 | + } |
| 115 | + if n > 3 { |
| 116 | + secret = urlAndMainAtrifactAndSecretName[3] |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Try downloading the artifcat |
| 121 | + msg, err := mc.DownloadArtifact(f, mainArtifact, secret) |
| 122 | + if err != nil { |
| 123 | + fmt.Printf("Got error when invoking Microcks client importing Artifact: %s", err) |
| 124 | + os.Exit(1) |
| 125 | + } |
| 126 | + fmt.Printf("Microcks has discovered '%s'\n", msg) |
| 127 | + } |
| 128 | +} |
0 commit comments