2023-03-09 04:34:49 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-03 04:39:48 +03:00
|
|
|
"context"
|
2024-07-30 06:40:24 +03:00
|
|
|
"encoding/base64"
|
2023-03-14 04:40:58 +03:00
|
|
|
"fmt"
|
2024-07-03 04:39:48 +03:00
|
|
|
"io"
|
|
|
|
"log/slog"
|
2023-10-20 05:18:23 +03:00
|
|
|
"os"
|
2023-03-14 04:40:58 +03:00
|
|
|
"strings"
|
|
|
|
|
2023-10-20 05:18:23 +03:00
|
|
|
"github.com/Azure/aztfexport/internal/cfgfile"
|
2024-07-03 04:39:48 +03:00
|
|
|
"github.com/Azure/aztfexport/internal/log"
|
2023-10-20 05:18:23 +03:00
|
|
|
"github.com/Azure/aztfexport/pkg/config"
|
|
|
|
"github.com/Azure/aztfexport/pkg/telemetry"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
|
2024-07-03 04:39:48 +03:00
|
|
|
azlog "github.com/Azure/azure-sdk-for-go/sdk/azcore/log"
|
2023-10-20 05:18:23 +03:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
|
|
|
|
"github.com/gofrs/uuid"
|
2023-03-09 04:34:49 +03:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var flagset FlagSet
|
|
|
|
|
|
|
|
type FlagSet struct {
|
|
|
|
// common flags
|
2023-07-26 03:21:33 +03:00
|
|
|
flagSubscriptionId string
|
|
|
|
flagOutputDir string
|
|
|
|
flagOverwrite bool
|
|
|
|
flagAppend bool
|
|
|
|
flagDevProvider bool
|
|
|
|
flagProviderVersion string
|
2023-10-20 05:18:23 +03:00
|
|
|
flagProviderName string
|
2023-07-26 03:21:33 +03:00
|
|
|
flagBackendType string
|
|
|
|
flagBackendConfig cli.StringSlice
|
|
|
|
flagFullConfig bool
|
2024-08-09 07:00:30 +03:00
|
|
|
flagMaskSensitive bool
|
2023-07-26 03:21:33 +03:00
|
|
|
flagParallelism int
|
|
|
|
flagContinue bool
|
|
|
|
flagNonInteractive bool
|
|
|
|
flagPlainUI bool
|
|
|
|
flagGenerateMappingFile bool
|
|
|
|
flagHCLOnly bool
|
|
|
|
flagModulePath string
|
2024-04-12 08:06:39 +03:00
|
|
|
flagGenerateImportBlock bool
|
2024-07-03 04:39:48 +03:00
|
|
|
flagLogPath string
|
|
|
|
flagLogLevel string
|
2023-07-26 03:21:33 +03:00
|
|
|
|
|
|
|
// common flags (auth)
|
2024-07-30 06:40:24 +03:00
|
|
|
flagEnv string
|
|
|
|
flagTenantId string
|
|
|
|
flagAuxiliaryTenantIds cli.StringSlice
|
|
|
|
flagClientId string
|
|
|
|
flagClientIdFilePath string
|
|
|
|
flagClientCertificate string
|
|
|
|
flagClientCertificatePath string
|
|
|
|
flagClientCertificatePassword string
|
|
|
|
flagClientSecret string
|
|
|
|
flagClientSecretFilePath string
|
|
|
|
flagOIDCRequestToken string
|
|
|
|
flagOIDCRequestURL string
|
|
|
|
flagOIDCTokenFilePath string
|
|
|
|
flagOIDCToken string
|
|
|
|
flagUseManagedIdentityCred bool
|
|
|
|
flagUseAzureCLICred bool
|
|
|
|
flagUseOIDCCred bool
|
2023-03-09 04:34:49 +03:00
|
|
|
|
|
|
|
// common flags (hidden)
|
2023-03-29 05:03:52 +03:00
|
|
|
hflagMockClient bool
|
|
|
|
hflagProfile string
|
|
|
|
hflagTFClientPluginPath string
|
2023-03-09 04:34:49 +03:00
|
|
|
|
|
|
|
// Subcommand specific flags
|
|
|
|
//
|
|
|
|
// res:
|
2024-08-09 11:11:52 +03:00
|
|
|
// flagResName (for single resource)
|
|
|
|
// flagResType (for single resource)
|
|
|
|
// flagPattern (for multi resources)
|
2023-03-09 04:34:49 +03:00
|
|
|
//
|
|
|
|
// rg:
|
|
|
|
// flagPattern
|
2023-11-03 04:50:56 +03:00
|
|
|
// flagIncludeRoleAssignment
|
2023-03-09 04:34:49 +03:00
|
|
|
//
|
|
|
|
// query:
|
|
|
|
// flagPattern
|
|
|
|
// flagRecursive
|
2023-11-03 04:50:56 +03:00
|
|
|
// flagIncludeRoleAssignment
|
|
|
|
// flagIncludeResourceGroup
|
2024-09-27 06:30:29 +03:00
|
|
|
// flagARGTable
|
|
|
|
// flagARGAuthorizationScopeFilter
|
|
|
|
flagPattern string
|
|
|
|
flagRecursive bool
|
|
|
|
flagResName string
|
|
|
|
flagResType string
|
|
|
|
flagIncludeRoleAssignment bool
|
|
|
|
flagIncludeResourceGroup bool
|
|
|
|
flagARGTable string
|
|
|
|
flagARGAuthorizationScopeFilter string
|
2023-03-09 04:34:49 +03:00
|
|
|
}
|
2023-03-14 04:40:58 +03:00
|
|
|
|
2024-08-09 11:11:52 +03:00
|
|
|
type Mode string
|
|
|
|
|
2023-03-14 04:40:58 +03:00
|
|
|
const (
|
2024-08-09 11:11:52 +03:00
|
|
|
ModeResource Mode = "resource"
|
|
|
|
ModeResourceGroup Mode = "resource-group"
|
|
|
|
ModeQuery Mode = "query"
|
|
|
|
ModeMappingFile Mode = "mapping-file"
|
2023-03-14 04:40:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// DescribeCLI construct a description of the CLI based on the flag set and the specified mode.
|
|
|
|
// The main reason is to record the usage of some "interesting" options in the telemetry.
|
|
|
|
// Note that only insensitive values are recorded (i.e. subscription id, resource id, etc are not recorded)
|
2024-08-09 11:11:52 +03:00
|
|
|
func (flag FlagSet) DescribeCLI(mode Mode) string {
|
|
|
|
args := []string{string(mode)}
|
2023-03-14 04:40:58 +03:00
|
|
|
|
|
|
|
// The following flags are skipped eiter not interesting, or might contain sensitive info:
|
|
|
|
// - flagOutputDir
|
|
|
|
// - flagDevProvider
|
|
|
|
// - flagBackendConfig
|
|
|
|
// - all hflags
|
|
|
|
|
2024-07-30 06:40:24 +03:00
|
|
|
if flag.flagSubscriptionId != "" {
|
|
|
|
args = append(args, "--subscription=*")
|
2023-03-15 09:30:17 +03:00
|
|
|
}
|
2023-03-14 04:40:58 +03:00
|
|
|
if flag.flagOverwrite {
|
|
|
|
args = append(args, "--overwrite=true")
|
|
|
|
}
|
|
|
|
if flag.flagAppend {
|
|
|
|
args = append(args, "--append=true")
|
|
|
|
}
|
2023-03-15 09:30:05 +03:00
|
|
|
if flag.flagProviderVersion != "" {
|
2023-11-08 12:31:00 +03:00
|
|
|
args = append(args, fmt.Sprintf(`-provider-version=%s`, flag.flagProviderVersion))
|
2023-03-15 09:30:05 +03:00
|
|
|
}
|
2023-10-24 06:12:16 +03:00
|
|
|
if flag.flagProviderName != "" {
|
2023-11-08 12:31:00 +03:00
|
|
|
args = append(args, fmt.Sprintf(`-provider-name=%s`, flag.flagProviderName))
|
2023-10-24 06:12:16 +03:00
|
|
|
}
|
2023-03-14 04:40:58 +03:00
|
|
|
if flag.flagBackendType != "" {
|
|
|
|
args = append(args, "--backend-type="+flag.flagBackendType)
|
|
|
|
}
|
|
|
|
if flag.flagFullConfig {
|
|
|
|
args = append(args, "--full-properties=true")
|
|
|
|
}
|
2024-08-09 07:00:30 +03:00
|
|
|
if flag.flagMaskSensitive {
|
|
|
|
args = append(args, "--mask-sensitive=true")
|
|
|
|
}
|
2023-03-14 04:40:58 +03:00
|
|
|
if flag.flagParallelism != 0 {
|
|
|
|
args = append(args, fmt.Sprintf("--parallelism=%d", flag.flagParallelism))
|
|
|
|
}
|
|
|
|
if flag.flagNonInteractive {
|
|
|
|
args = append(args, "--non-interactive=true")
|
|
|
|
}
|
2023-07-10 04:46:29 +03:00
|
|
|
if flag.flagPlainUI {
|
|
|
|
args = append(args, "--plain-ui=true")
|
|
|
|
}
|
2023-03-14 04:40:58 +03:00
|
|
|
if flag.flagContinue {
|
|
|
|
args = append(args, "--continue=true")
|
|
|
|
}
|
|
|
|
if flag.flagGenerateMappingFile {
|
|
|
|
args = append(args, "--generate-mapping-file=true")
|
|
|
|
}
|
|
|
|
if flag.flagHCLOnly {
|
|
|
|
args = append(args, "--hcl-only=true")
|
|
|
|
}
|
|
|
|
if flag.flagModulePath != "" {
|
|
|
|
args = append(args, "--module-path="+flag.flagModulePath)
|
|
|
|
}
|
2024-04-12 08:06:39 +03:00
|
|
|
if !flag.flagGenerateImportBlock {
|
|
|
|
args = append(args, "--generate-import-block=true")
|
|
|
|
}
|
2023-07-26 03:21:33 +03:00
|
|
|
|
2024-07-30 06:40:24 +03:00
|
|
|
if flag.flagEnv != "" {
|
|
|
|
args = append(args, "--env="+flag.flagEnv)
|
2023-07-26 03:21:33 +03:00
|
|
|
}
|
2024-07-30 06:40:24 +03:00
|
|
|
if flag.flagTenantId != "" {
|
|
|
|
args = append(args, "--tenant-id=*")
|
2023-07-26 03:21:33 +03:00
|
|
|
}
|
2024-07-30 06:40:24 +03:00
|
|
|
if v := flag.flagAuxiliaryTenantIds.Value(); len(v) != 0 {
|
|
|
|
args = append(args, fmt.Sprintf("--tenant-id=[%d]", len(v)))
|
2023-07-26 03:21:33 +03:00
|
|
|
}
|
2024-07-30 06:40:24 +03:00
|
|
|
if flag.flagClientId != "" {
|
|
|
|
args = append(args, "--client-id=*")
|
|
|
|
}
|
|
|
|
if flag.flagClientIdFilePath != "" {
|
|
|
|
args = append(args, "--client-id-file-path="+flag.flagClientIdFilePath)
|
|
|
|
}
|
|
|
|
if flag.flagClientCertificate != "" {
|
|
|
|
args = append(args, "--client-certificate=*")
|
|
|
|
}
|
|
|
|
if flag.flagClientCertificatePath != "" {
|
|
|
|
args = append(args, "--client-certificate-path="+flag.flagClientCertificatePath)
|
|
|
|
}
|
|
|
|
if flag.flagClientCertificatePassword != "" {
|
|
|
|
args = append(args, "--client-certificate-password=*")
|
|
|
|
}
|
|
|
|
if flag.flagClientSecret != "" {
|
|
|
|
args = append(args, "--client-secret=***")
|
|
|
|
}
|
|
|
|
if flag.flagClientSecretFilePath != "" {
|
|
|
|
args = append(args, "--client-secret-file-path="+flag.flagClientSecretFilePath)
|
2023-07-26 03:21:33 +03:00
|
|
|
}
|
|
|
|
if flag.flagOIDCRequestToken != "" {
|
|
|
|
args = append(args, "--oidc-request-token=*")
|
|
|
|
}
|
|
|
|
if flag.flagOIDCRequestURL != "" {
|
|
|
|
args = append(args, "--oidc-request-url="+flag.flagOIDCRequestURL)
|
|
|
|
}
|
|
|
|
if flag.flagOIDCTokenFilePath != "" {
|
|
|
|
args = append(args, "--oidc-token-file-path="+flag.flagOIDCTokenFilePath)
|
|
|
|
}
|
|
|
|
if flag.flagOIDCToken != "" {
|
|
|
|
args = append(args, "--oidc-token=*")
|
|
|
|
}
|
2024-07-30 06:40:24 +03:00
|
|
|
if flag.flagUseManagedIdentityCred {
|
|
|
|
args = append(args, "--use-managed-identity-cred=true")
|
|
|
|
}
|
|
|
|
if flag.flagUseAzureCLICred {
|
|
|
|
args = append(args, "--use-azure-cli-cred=true")
|
|
|
|
}
|
|
|
|
if flag.flagUseOIDCCred {
|
|
|
|
args = append(args, "--use-oidc-cred=true")
|
|
|
|
}
|
2023-07-26 03:21:33 +03:00
|
|
|
|
|
|
|
if flag.hflagTFClientPluginPath != "" {
|
|
|
|
args = append(args, "--tfclient-plugin-path="+flag.hflagTFClientPluginPath)
|
|
|
|
}
|
2023-03-14 04:40:58 +03:00
|
|
|
switch mode {
|
|
|
|
case ModeResource:
|
|
|
|
if flag.flagResName != "" {
|
|
|
|
args = append(args, "--name="+flag.flagResName)
|
|
|
|
}
|
|
|
|
if flag.flagResType != "" {
|
|
|
|
args = append(args, "--type="+flag.flagResType)
|
|
|
|
}
|
2024-08-09 11:11:52 +03:00
|
|
|
if flag.flagPattern != "" {
|
|
|
|
args = append(args, "--name-pattern="+flag.flagPattern)
|
|
|
|
}
|
2023-03-14 04:40:58 +03:00
|
|
|
case ModeResourceGroup:
|
|
|
|
if flag.flagPattern != "" {
|
|
|
|
args = append(args, "--name-pattern="+flag.flagPattern)
|
|
|
|
}
|
2023-11-03 04:50:56 +03:00
|
|
|
if flag.flagIncludeRoleAssignment {
|
|
|
|
args = append(args, "--include-role-assignment=true")
|
|
|
|
}
|
2023-03-14 04:40:58 +03:00
|
|
|
case ModeQuery:
|
|
|
|
if flag.flagPattern != "" {
|
|
|
|
args = append(args, "--name-pattern="+flag.flagPattern)
|
|
|
|
}
|
|
|
|
if flag.flagRecursive {
|
|
|
|
args = append(args, "--recursive=true")
|
|
|
|
}
|
2023-11-03 04:50:56 +03:00
|
|
|
if flag.flagIncludeRoleAssignment {
|
|
|
|
args = append(args, "--include-role-assignment=true")
|
|
|
|
}
|
|
|
|
if flag.flagIncludeResourceGroup {
|
|
|
|
args = append(args, "--include-resource-group=true")
|
|
|
|
}
|
2024-09-27 06:30:29 +03:00
|
|
|
if flag.flagARGTable != "" {
|
|
|
|
args = append(args, "--arg-table="+flag.flagARGTable)
|
|
|
|
}
|
|
|
|
if flag.flagARGAuthorizationScopeFilter != "" {
|
|
|
|
args = append(args, "--arg-authorization-scope-filter="+flag.flagARGAuthorizationScopeFilter)
|
|
|
|
}
|
2023-03-14 04:40:58 +03:00
|
|
|
}
|
|
|
|
return "aztfexport " + strings.Join(args, " ")
|
|
|
|
}
|
2023-10-20 05:18:23 +03:00
|
|
|
|
|
|
|
func initTelemetryClient(subscriptionId string) telemetry.Client {
|
|
|
|
cfg, err := cfgfile.GetConfig()
|
|
|
|
if err != nil {
|
|
|
|
return telemetry.NewNullClient()
|
|
|
|
}
|
|
|
|
enabled, installId := cfg.TelemetryEnabled, cfg.InstallationId
|
|
|
|
if !enabled {
|
|
|
|
return telemetry.NewNullClient()
|
|
|
|
}
|
|
|
|
if installId == "" {
|
|
|
|
uuid, err := uuid.NewV4()
|
|
|
|
if err == nil {
|
|
|
|
installId = uuid.String()
|
|
|
|
} else {
|
|
|
|
installId = "undefined"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionId := "undefined"
|
|
|
|
if uuid, err := uuid.NewV4(); err == nil {
|
|
|
|
sessionId = uuid.String()
|
|
|
|
}
|
|
|
|
return telemetry.NewAppInsight(subscriptionId, installId, sessionId)
|
|
|
|
}
|
|
|
|
|
2024-07-30 06:40:24 +03:00
|
|
|
func (f FlagSet) buildAuthConfig() (*config.AuthConfig, error) {
|
|
|
|
clientId := f.flagClientId
|
|
|
|
if path := f.flagClientIdFilePath; path != "" {
|
2024-09-05 05:53:18 +03:00
|
|
|
// #nosec G304
|
2024-07-30 06:40:24 +03:00
|
|
|
b, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading Client ID from file %q: %v", path, err)
|
|
|
|
}
|
|
|
|
clientId = string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientSecret := f.flagClientSecret
|
|
|
|
if path := f.flagClientSecretFilePath; path != "" {
|
2024-09-05 05:53:18 +03:00
|
|
|
// #nosec G304
|
2024-07-30 06:40:24 +03:00
|
|
|
b, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading Client secret from file %q: %v", path, err)
|
|
|
|
}
|
|
|
|
clientSecret = string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientCertEncoded := f.flagClientCertificate
|
|
|
|
if path := f.flagClientCertificatePath; path != "" {
|
2024-09-05 05:53:18 +03:00
|
|
|
// #nosec G304
|
2024-07-30 06:40:24 +03:00
|
|
|
b, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading Client certificate from file %q: %v", path, err)
|
|
|
|
}
|
|
|
|
clientCertEncoded = base64.StdEncoding.EncodeToString(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
oidcToken := f.flagOIDCToken
|
|
|
|
if path := f.flagOIDCTokenFilePath; path != "" {
|
2024-09-05 05:53:18 +03:00
|
|
|
// #nosec G304
|
2024-07-30 06:40:24 +03:00
|
|
|
b, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading OIDC token from file %q: %v", path, err)
|
|
|
|
}
|
|
|
|
oidcToken = strings.TrimSpace(string(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
c := config.AuthConfig{
|
|
|
|
Environment: f.flagEnv,
|
|
|
|
TenantID: f.flagTenantId,
|
|
|
|
AuxiliaryTenantIDs: f.flagAuxiliaryTenantIds.Value(),
|
|
|
|
ClientID: clientId,
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
ClientCertificateEncoded: clientCertEncoded,
|
|
|
|
ClientCertificatePassword: f.flagClientCertificatePassword,
|
|
|
|
OIDCTokenRequestToken: f.flagOIDCRequestToken,
|
|
|
|
OIDCTokenRequestURL: f.flagOIDCRequestURL,
|
|
|
|
OIDCAssertionToken: oidcToken,
|
|
|
|
UseAzureCLI: f.flagUseAzureCLICred,
|
|
|
|
UseManagedIdentity: f.flagUseManagedIdentityCred,
|
|
|
|
UseOIDC: f.flagUseOIDCCred,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildCommonConfig builds the CommonConfig from the FlagSet, except the TFClient, which is built afterwards as it requires a logger.
|
|
|
|
func (f FlagSet) BuildCommonConfig() (config.CommonConfig, error) {
|
|
|
|
// Logger is only enabled when the log path is specified.
|
|
|
|
// This is because either interactive/non-interactive mode controls the terminal rendering,
|
|
|
|
// logging to stdout/stderr will impact the rendering.
|
|
|
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
|
|
if path := f.flagLogPath; path != "" {
|
|
|
|
level, err := logLevel(f.flagLogLevel)
|
|
|
|
if err != nil {
|
|
|
|
return config.CommonConfig{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// #nosec G304
|
|
|
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return config.CommonConfig{}, fmt.Errorf("creating log file %s: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger = slog.New(slog.NewTextHandler(f, &slog.HandlerOptions{Level: level}))
|
|
|
|
|
|
|
|
// Enable log for azure sdk
|
|
|
|
os.Setenv("AZURE_SDK_GO_LOGGING", "all") // #nosec G104
|
|
|
|
azlog.SetListener(func(cls azlog.Event, msg string) {
|
|
|
|
logger.Log(context.Background(), log.LevelTrace, msg, "event", cls)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
authConfig, err := f.buildAuthConfig()
|
|
|
|
if err != nil {
|
|
|
|
return config.CommonConfig{}, err
|
|
|
|
}
|
|
|
|
|
2023-10-20 05:18:23 +03:00
|
|
|
var cloudCfg cloud.Configuration
|
2024-07-30 06:40:24 +03:00
|
|
|
switch env := f.flagEnv; strings.ToLower(env) {
|
2023-10-20 05:18:23 +03:00
|
|
|
case "public":
|
|
|
|
cloudCfg = cloud.AzurePublic
|
|
|
|
case "usgovernment":
|
|
|
|
cloudCfg = cloud.AzureGovernment
|
|
|
|
case "china":
|
|
|
|
cloudCfg = cloud.AzureChina
|
|
|
|
default:
|
2024-07-30 06:40:24 +03:00
|
|
|
return config.CommonConfig{}, fmt.Errorf("unknown environment specified: %q", env)
|
2023-10-20 05:18:23 +03:00
|
|
|
}
|
|
|
|
|
2024-07-30 06:40:24 +03:00
|
|
|
clientOpt := arm.ClientOptions{
|
2023-10-20 05:18:23 +03:00
|
|
|
ClientOptions: policy.ClientOptions{
|
|
|
|
Cloud: cloudCfg,
|
|
|
|
Telemetry: policy.TelemetryOptions{
|
2024-07-30 06:40:24 +03:00
|
|
|
ApplicationID: fmt.Sprintf("aztfexport(%s)", f.flagProviderName),
|
2023-10-20 05:18:23 +03:00
|
|
|
Disabled: false,
|
|
|
|
},
|
|
|
|
Logging: policy.LogOptions{
|
|
|
|
IncludeBody: true,
|
|
|
|
},
|
|
|
|
},
|
2024-07-30 06:40:24 +03:00
|
|
|
AuxiliaryTenants: authConfig.AuxiliaryTenantIDs,
|
|
|
|
DisableRPRegistration: true,
|
2023-10-20 05:18:23 +03:00
|
|
|
}
|
|
|
|
|
2024-07-30 06:40:24 +03:00
|
|
|
cred, err := NewDefaultAzureCredential(*logger, &DefaultAzureCredentialOptions{
|
|
|
|
AuthConfig: *authConfig,
|
|
|
|
ClientOptions: clientOpt.ClientOptions,
|
|
|
|
DisableInstanceDiscovery: false,
|
|
|
|
SendCertificateChain: false,
|
|
|
|
})
|
2023-10-20 05:18:23 +03:00
|
|
|
if err != nil {
|
2024-07-30 06:40:24 +03:00
|
|
|
return config.CommonConfig{}, fmt.Errorf("failed to new credential: %v", err)
|
2023-10-20 05:18:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg := config.CommonConfig{
|
2024-07-30 06:40:24 +03:00
|
|
|
Logger: logger,
|
|
|
|
AuthConfig: *authConfig,
|
2023-10-20 05:18:23 +03:00
|
|
|
SubscriptionId: f.flagSubscriptionId,
|
|
|
|
AzureSDKCredential: cred,
|
2024-07-30 06:40:24 +03:00
|
|
|
AzureSDKClientOption: clientOpt,
|
2023-10-20 05:18:23 +03:00
|
|
|
OutputDir: f.flagOutputDir,
|
|
|
|
ProviderVersion: f.flagProviderVersion,
|
|
|
|
ProviderName: f.flagProviderName,
|
|
|
|
DevProvider: f.flagDevProvider,
|
|
|
|
ContinueOnError: f.flagContinue,
|
|
|
|
BackendType: f.flagBackendType,
|
|
|
|
BackendConfig: f.flagBackendConfig.Value(),
|
|
|
|
FullConfig: f.flagFullConfig,
|
2024-08-09 07:00:30 +03:00
|
|
|
MaskSensitive: f.flagMaskSensitive,
|
2023-10-20 05:18:23 +03:00
|
|
|
Parallelism: f.flagParallelism,
|
|
|
|
HCLOnly: f.flagHCLOnly,
|
|
|
|
ModulePath: f.flagModulePath,
|
2024-04-12 08:06:39 +03:00
|
|
|
GenerateImportBlock: f.flagGenerateImportBlock,
|
2023-10-20 05:18:23 +03:00
|
|
|
TelemetryClient: initTelemetryClient(f.flagSubscriptionId),
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.flagAppend {
|
|
|
|
cfg.OutputFileNames = config.OutputFileNames{
|
|
|
|
TerraformFileName: "terraform.aztfexport.tf",
|
|
|
|
ProviderFileName: "provider.aztfexport.tf",
|
|
|
|
MainFileName: "main.aztfexport.tf",
|
|
|
|
ImportBlockFileName: "import.aztfexport.tf",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
2024-07-03 04:39:48 +03:00
|
|
|
|
|
|
|
func logLevel(level string) (slog.Level, error) {
|
|
|
|
switch strings.ToUpper(level) {
|
|
|
|
case "ERROR":
|
|
|
|
return slog.LevelError, nil
|
|
|
|
case "WARN":
|
|
|
|
return slog.LevelWarn, nil
|
|
|
|
case "INFO":
|
|
|
|
return slog.LevelInfo, nil
|
|
|
|
case "DEBUG":
|
|
|
|
return slog.LevelDebug, nil
|
|
|
|
case "TRACE":
|
|
|
|
return log.LevelTrace, nil
|
|
|
|
default:
|
|
|
|
return slog.Level(0), fmt.Errorf("unknown log level: %s", level)
|
|
|
|
}
|
|
|
|
}
|