int get_authinfo_data()

in turbonfs/src/main.cpp [433:502]


int get_authinfo_data(struct auth_info& auth_info)
{
    // We should not be here without a valid account. 
    assert(aznfsc_cfg.account != nullptr);

    std::string output = run_command("az account show --output json");
    if (output.empty()) {
        AZLogError("'az account show --output json' failed to get account details");
        // User is required to perform 'az login'.
        is_azlogin_required = true;
        return -1;
    }

    // Extract tenantid, subscriptionid, and user details from the output json.
    try {
        const auto json_data = json::parse(output);

        auth_info.tenantid = json_data["tenantId"].get<std::string>();
        auth_info.subscriptionid = json_data["id"].get<std::string>();
        auth_info.username = json_data["user"]["name"].get<std::string>();
        auth_info.usertype = json_data["user"]["type"].get<std::string>();
    } catch (json::parse_error& ev) {
        AZLogError("Failed to parse json: {}, error: {}", output, ev.what());
        return -1;
    }

    const std::string command = std::string("az storage account show -n ") + std::string(aznfsc_cfg.account);
    output = run_command(command);
    if (output.empty()) {
        AZLogError("'{}' failed to get storage account details", command);
        status_pipe_error_string = "Storage account '" + std::string(aznfsc_cfg.account) + 
                                   "' not found in the subscription " + auth_info.subscriptionid;
        return -1;
    }

    // Extract resource group from the output json.
    try {
        const auto json_data = json::parse(output);
        auth_info.resourcegroupname = json_data["resourceGroup"].get<std::string>();
    } catch (json::parse_error& ev) {
        AZLogError("Failed to parse json: {}, error: {}", output, ev.what());
        return -1;
    }

    // Caller expects valid values for tenantid, subscriptionid and resourcegroupname.
    if (auth_info.tenantid.empty() ||
        auth_info.subscriptionid.empty() ||
        auth_info.resourcegroupname.empty()) {
        AZLogError("Invalid authdata parameters returned from azcli commands: "
                   "tenantid: {} subscriptionid: {} username: {} "
                   "usertype: {} resourcegroupname: {}",
                   auth_info.tenantid,
                   auth_info.subscriptionid,
                   auth_info.username,
                   auth_info.usertype,
                   auth_info.resourcegroupname);
        return -1;
    }

    AZLogDebug("Authdata parameters returned from azcli commands: "
               "tenantid: {} subscriptionid: {} username: {} "
               "usertype: {} resourcegroupname: {}",
               auth_info.tenantid,
               auth_info.subscriptionid,
               auth_info.username,
               auth_info.usertype,
               auth_info.resourcegroupname);

    return 0;
}