2023-10-18 11:56:55 +00:00
|
|
|
package main
|
|
|
|
|
2023-10-18 14:15:10 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2023-10-19 09:10:16 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2023-10-18 14:15:10 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
AdminUser = "admin"
|
2023-10-19 00:39:11 +00:00
|
|
|
EndpointURL = "http://localhost:9000/api/endpoints"
|
|
|
|
AuthURL = "http://localhost:9000/api/auth"
|
2023-10-18 14:15:10 +00:00
|
|
|
CredentialLoc = "/data/credential"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Endpoint struct {
|
2023-10-19 09:10:16 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type EndpointCreation struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
EndpointCreationType int `json:"EndpointCreationType"`
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AuthResponse struct {
|
|
|
|
Jwt string `json:"jwt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Credentials struct {
|
2023-10-19 09:10:16 +00:00
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
2023-10-18 11:56:55 +00:00
|
|
|
|
|
|
|
func main() {
|
2023-10-19 09:34:17 +00:00
|
|
|
|
|
|
|
fmt.Println("Start to create endpoint...")
|
2023-10-19 09:10:16 +00:00
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
password, err := getPassword()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to get password:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := authenticate(client, AdminUser, password)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to authenticate:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints, err := queryEndpoints(client, token)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to query endpoints:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, endpoint := range endpoints {
|
|
|
|
if endpoint.Name == "local" && endpoint.URL == "unix:///var/run/docker.sock" {
|
|
|
|
fmt.Println("Endpoint exists, exiting...")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-10-18 14:15:10 +00:00
|
|
|
|
2023-10-19 09:10:16 +00:00
|
|
|
fmt.Println("Endpoint does not exist, creating...")
|
|
|
|
createEndpoint(client, token)
|
|
|
|
|
|
|
|
fmt.Println("Endpoint created successfully")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPassword() (string, error) {
|
2023-10-18 14:15:10 +00:00
|
|
|
for {
|
|
|
|
if _, err := os.Stat(CredentialLoc); os.IsNotExist(err) {
|
|
|
|
fmt.Printf("%s does not exist, waiting for 3 seconds...\n", CredentialLoc)
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s exists, proceeding...\n", CredentialLoc)
|
|
|
|
data, err := ioutil.ReadFile(CredentialLoc)
|
|
|
|
if err != nil {
|
2023-10-19 09:10:16 +00:00
|
|
|
return "", err
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
2023-10-19 09:10:16 +00:00
|
|
|
return string(data), nil
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-19 09:10:16 +00:00
|
|
|
}
|
2023-10-18 14:15:10 +00:00
|
|
|
|
2023-10-19 09:10:16 +00:00
|
|
|
func authenticate(client *http.Client, username, password string) (string, error) {
|
|
|
|
credentials := Credentials{Username: username, Password: password}
|
2023-10-18 14:15:10 +00:00
|
|
|
credentialsJson, err := json.Marshal(credentials)
|
|
|
|
if err != nil {
|
2023-10-19 09:10:16 +00:00
|
|
|
return "", err
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", AuthURL, bytes.NewBuffer(credentialsJson))
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2023-10-19 09:10:16 +00:00
|
|
|
return "", err
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2023-10-19 09:10:16 +00:00
|
|
|
|
2023-10-18 14:15:10 +00:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2023-10-19 09:10:16 +00:00
|
|
|
return "", err
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var authResponse AuthResponse
|
|
|
|
err = json.Unmarshal(body, &authResponse)
|
|
|
|
if err != nil {
|
2023-10-19 09:10:16 +00:00
|
|
|
return "", err
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 09:10:16 +00:00
|
|
|
return authResponse.Jwt, nil
|
|
|
|
}
|
2023-10-18 14:15:10 +00:00
|
|
|
|
2023-10-19 09:10:16 +00:00
|
|
|
func queryEndpoints(client *http.Client, token string) ([]Endpoint, error) {
|
|
|
|
req, err := http.NewRequest("GET", EndpointURL, nil)
|
|
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
|
|
resp, err := client.Do(req)
|
2023-10-18 14:15:10 +00:00
|
|
|
if err != nil {
|
2023-10-19 09:10:16 +00:00
|
|
|
return nil, err
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2023-10-19 09:10:16 +00:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2023-10-18 14:15:10 +00:00
|
|
|
if err != nil {
|
2023-10-19 09:10:16 +00:00
|
|
|
return nil, err
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var endpoints []Endpoint
|
|
|
|
err = json.Unmarshal(body, &endpoints)
|
|
|
|
if err != nil {
|
2023-10-19 09:10:16 +00:00
|
|
|
return nil, err
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 09:10:16 +00:00
|
|
|
return endpoints, nil
|
|
|
|
}
|
2023-10-18 14:15:10 +00:00
|
|
|
|
2023-10-19 09:10:16 +00:00
|
|
|
func createEndpoint(client *http.Client, token string) error {
|
|
|
|
data := url.Values{
|
|
|
|
"Name": {"local"},
|
|
|
|
"EndpointCreationType": {"1"},
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 09:10:16 +00:00
|
|
|
req, err := http.NewRequest("POST", EndpointURL, strings.NewReader(data.Encode()))
|
2023-10-18 14:15:10 +00:00
|
|
|
if err != nil {
|
2023-10-19 09:10:16 +00:00
|
|
|
return err
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 09:10:16 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
2023-10-18 14:15:10 +00:00
|
|
|
if resp.StatusCode != http.StatusCreated {
|
2023-10-19 09:10:16 +00:00
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
return fmt.Errorf("Failed to create endpoint: %s, Response body: %s", resp.Status, string(body))
|
2023-10-18 14:15:10 +00:00
|
|
|
}
|
2023-10-19 09:10:16 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|