mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
feat(az): configure the az CLI against floci-az with floci az setup (#29)
This commit is contained in:
1 parent
dd38f6dc27
commit
7b58a38c5a
27 files changed
+1587
-12
No files matched your search
@@ -2,8 +2,10 @@ package io.floci.cli.commands.az;
|
||||
|
||||
import io.floci.cli.GlobalOptions;
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.config.InstanceState;
|
||||
import io.floci.cli.docker.DockerClient;
|
||||
import io.floci.cli.doctor.checks.AzCliConnectionStringCheck;
|
||||
import io.floci.cli.http.FlociHttpClient;
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.output.OutputFormat;
|
||||
import io.floci.cli.output.Printer;
|
||||
@@ -11,6 +13,8 @@ import io.floci.cli.output.ShellExport;
|
||||
import picocli.CommandLine.*;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -64,6 +68,23 @@ public class AzEnvCommand implements Callable<Integer> {
|
||||
paramLabel = "bash|fish|powershell")
|
||||
String shell;
|
||||
|
||||
private final Path stateRoot;
|
||||
private final AzSetupCommand.CertSource certSource;
|
||||
|
||||
public AzEnvCommand() {
|
||||
this(InstanceState.defaultRoot(),
|
||||
endpoint -> new FlociHttpClient(endpoint, ProductProfile.AZ.controlPrefix()).tlsCert());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test seam: {@code stateRoot} stands in for {@code ~/.floci}, where 'floci az setup' wrote
|
||||
* each instance's config dir and CA; {@code certSource} reads the running server's CA.
|
||||
*/
|
||||
public AzEnvCommand(Path stateRoot, AzSetupCommand.CertSource certSource) {
|
||||
this.stateRoot = stateRoot;
|
||||
this.certSource = certSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer call() {
|
||||
Printer printer = global.printer();
|
||||
@@ -72,6 +93,7 @@ public class AzEnvCommand implements Callable<Integer> {
|
||||
|
||||
boolean sdkVarsMode = "sdk-vars".equals(format) || serviceFilter != null;
|
||||
List<String> requestedServices = resolveServices();
|
||||
Map<String, String> azCliVars = azCliVars(printer, effectiveEndpoint);
|
||||
|
||||
if (printer.format() != OutputFormat.text) {
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
@@ -85,6 +107,7 @@ public class AzEnvCommand implements Callable<Integer> {
|
||||
out.put(envVarName(svc), buildEndpointUrl(svc, host, port, account));
|
||||
}
|
||||
}
|
||||
out.putAll(azCliVars);
|
||||
printer.structured(out);
|
||||
return 0;
|
||||
}
|
||||
@@ -92,6 +115,7 @@ public class AzEnvCommand implements Callable<Integer> {
|
||||
if (!sdkVarsMode) {
|
||||
String connStr = AzCliConnectionStringCheck.buildConnectionString(account, host, port);
|
||||
printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_CONNECTION_STRING", connStr));
|
||||
printAzCliVars(printer, azCliVars);
|
||||
printer.println("");
|
||||
printer.println(Ansi.gray("# Run: eval $(floci az env)"));
|
||||
} else {
|
||||
@@ -100,6 +124,7 @@ public class AzEnvCommand implements Callable<Integer> {
|
||||
for (String svc : requestedServices) {
|
||||
printer.println(ShellExport.formatExport(shell, envVarName(svc), buildEndpointUrl(svc, host, port, account)));
|
||||
}
|
||||
printAzCliVars(printer, azCliVars);
|
||||
printer.println("");
|
||||
printer.println(Ansi.gray("# Run: eval $(floci az env --format sdk-vars)"));
|
||||
}
|
||||
@@ -107,6 +132,56 @@ public class AzEnvCommand implements Callable<Integer> {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Only after 'floci az setup' has run: point a plain 'az' in this shell at the isolated config
|
||||
// and make it trust floci-az's certificate. A --global setup leaves no config dir, so the
|
||||
// bundle is exported and AZURE_CONFIG_DIR is cleared (a null value).
|
||||
// The state is per instance (keyed by container), so --profile and --container pick the
|
||||
// setup that belongs to the emulator this env points at.
|
||||
private Map<String, String> azCliVars(Printer printer, String endpoint) {
|
||||
Map<String, String> vars = new LinkedHashMap<>();
|
||||
Path dir;
|
||||
try {
|
||||
dir = InstanceState.dir(stateRoot, ProductProfile.AZ, global.container);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return vars; // not a usable container name; the storage variables still stand
|
||||
}
|
||||
Path configDir = dir.resolve(AzSetupCommand.CONFIG_DIR_NAME);
|
||||
Path bundle = dir.resolve(AzSetupCommand.BUNDLE_NAME);
|
||||
if (Files.isDirectory(configDir)) {
|
||||
vars.put("AZURE_CONFIG_DIR", configDir.toAbsolutePath().toString());
|
||||
} else if (Files.isRegularFile(bundle)) {
|
||||
// Set up with --global: clear an AZURE_CONFIG_DIR a previous 'eval $(floci az env)'
|
||||
// left in this shell, or plain 'az' keeps using that other instance's isolated login.
|
||||
vars.put("AZURE_CONFIG_DIR", null);
|
||||
}
|
||||
if (Files.isRegularFile(bundle)) vars.put("REQUESTS_CA_BUNDLE", bundle.toAbsolutePath().toString());
|
||||
warnIfCertificateChanged(printer, dir.resolve(AzSetupCommand.CA_NAME), endpoint);
|
||||
return vars;
|
||||
}
|
||||
|
||||
// A container recreated without a persist dir generates a new CA, and the saved bundle then
|
||||
// fails every az call with a TLS error that says nothing about why. Warn on stderr, which
|
||||
// 'eval $(floci az env)' leaves visible. Any failure to fetch stays silent: env must keep
|
||||
// working with the emulator stopped.
|
||||
private void warnIfCertificateChanged(Printer printer, Path savedCa, String endpoint) {
|
||||
if (!Files.isRegularFile(savedCa)) return;
|
||||
try {
|
||||
String live = certSource.fetch(endpoint);
|
||||
if (!live.strip().equals(Files.readString(savedCa).strip())) {
|
||||
printer.warn("The Floci Azure certificate changed since 'floci az setup' ran.\n"
|
||||
+ "Run 'floci az setup" + global.instanceSelector() + "' to trust the new one.");
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void printAzCliVars(Printer printer, Map<String, String> vars) {
|
||||
vars.forEach((key, value) -> printer.println(value == null
|
||||
? ShellExport.formatUnset(shell, key)
|
||||
: ShellExport.formatExport(shell, key, value)));
|
||||
}
|
||||
|
||||
private List<String> resolveServices() {
|
||||
if (serviceFilter != null && !serviceFilter.isBlank()) {
|
||||
List<String> result = new ArrayList<>();
|
||||
|
||||
Reference in new issue
Block a user