[GH-25995]_Validate SITE URL in mmctl auth login (#30362)

Этот коммит содержится в:
KIMBOH LOVETTE
2025-05-07 11:24:18 +01:00
коммит произвёл GitHub
родитель 5b68afe452
Коммит b8b3efda48

Просмотреть файл

@@ -7,6 +7,8 @@ import (
"bufio" "bufio"
"context" "context"
"fmt" "fmt"
"net/http"
"net/url"
"os" "os"
"sort" "sort"
"strings" "strings"
@@ -164,7 +166,20 @@ func loginCmdF(cmd *cobra.Command, args []string) error {
allowInsecureSHA1 := viper.GetBool("insecure-sha1-intermediate") allowInsecureSHA1 := viper.GetBool("insecure-sha1-intermediate")
allowInsecureTLS := viper.GetBool("insecure-tls-version") allowInsecureTLS := viper.GetBool("insecure-tls-version")
url := strings.TrimRight(args[0], "/") instanceURL := strings.TrimRight(args[0], "/")
_, err = url.ParseRequestURI(instanceURL)
if err != nil {
return fmt.Errorf("could not parse the instance url: %w", err)
}
res, err := http.Get(instanceURL)
if err != nil {
return fmt.Errorf("could not get instance status: %w", err)
}
if res.StatusCode != 200 {
return fmt.Errorf("instance status code is not 200: %d", res.StatusCode)
}
method := MethodPassword method := MethodPassword
ctx := context.TODO() ctx := context.TODO()
@@ -206,10 +221,10 @@ func loginCmdF(cmd *cobra.Command, args []string) error {
var c *model.Client4 var c *model.Client4
var err error var err error
if mfaToken != "" { if mfaToken != "" {
c, _, err = InitClientWithMFA(ctx, username, password, mfaToken, url, allowInsecureSHA1, allowInsecureTLS) c, _, err = InitClientWithMFA(ctx, username, password, mfaToken, instanceURL, allowInsecureSHA1, allowInsecureTLS)
method = MethodMFA method = MethodMFA
} else { } else {
c, _, err = InitClientWithUsernameAndPassword(ctx, username, password, url, allowInsecureSHA1, allowInsecureTLS) c, _, err = InitClientWithUsernameAndPassword(ctx, username, password, instanceURL, allowInsecureSHA1, allowInsecureTLS)
} }
if err != nil { if err != nil {
return fmt.Errorf("could not initiate client: %w", err) return fmt.Errorf("could not initiate client: %w", err)
@@ -219,7 +234,7 @@ func loginCmdF(cmd *cobra.Command, args []string) error {
username = "Personal Access Token" username = "Personal Access Token"
method = MethodToken method = MethodToken
credentials := Credentials{ credentials := Credentials{
InstanceURL: url, InstanceURL: instanceURL,
AuthToken: accessToken, AuthToken: accessToken,
} }
if _, _, err := InitClientWithCredentials(ctx, &credentials, allowInsecureSHA1, allowInsecureTLS); err != nil { if _, _, err := InitClientWithCredentials(ctx, &credentials, allowInsecureSHA1, allowInsecureTLS); err != nil {
@@ -229,7 +244,7 @@ func loginCmdF(cmd *cobra.Command, args []string) error {
credentials := Credentials{ credentials := Credentials{
Name: name, Name: name,
InstanceURL: url, InstanceURL: instanceURL,
Username: username, Username: username,
AuthToken: accessToken, AuthToken: accessToken,
AuthMethod: method, AuthMethod: method,
@@ -246,7 +261,7 @@ func loginCmdF(cmd *cobra.Command, args []string) error {
} }
} }
printer.Print(fmt.Sprintf("\n credentials for %q: \"%s@%s\" stored\n", name, username, url)) printer.Print(fmt.Sprintf("\n credentials for %q: \"%s@%s\" stored\n", name, username, instanceURL))
return nil return nil
} }