Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for proxy #921

Closed
DevAcc24 opened this issue Feb 7, 2025 · 2 comments
Closed

Add support for proxy #921

DevAcc24 opened this issue Feb 7, 2025 · 2 comments

Comments

@DevAcc24
Copy link

DevAcc24 commented Feb 7, 2025

Is your feature request related to a problem? Please describe.
It is impossible to use SWA-CLI to deploy application when organization forces to use proxy.
Part of the problem related to node-fetch used (for example) to load StaticSitesClient. This could be solved by using http-proxy-agent & https-proxy-agent (see sample fetch-proxy.ts below). In this case SWA-CLI could be used when --deployment-token explicitly specified on command line so there is no need to fetch is from SWA itself.
Another part of the same problem with @azure/arm-appservice dependency of @azure/static-web-apps-cli making it impossible to fetch information about existing applications from Azure.

Describe the solution you'd like
It would be great if SWA-CLI use HTTP_PROXY & HTTPS_PROXY environment variables when communicating with Azure services.

Describe alternatives you've considered
Add additional optional command line parameters like --http-proxy and --https-proxy so could explicitly specify proxy to be used for communication.

Additional context
Below command output on Win11.
Deploy command without --deployment-token parameter.

C:\Dev\myapp>npx swa deploy --verbose=silly

Welcome to Azure Static Web Apps CLI (2.0.3)

Getting config file options from "swa-cli.config.json"...
Config file does not exist at "swa-cli.config.json"
Resolving outputLocation=. full path...
Deploying front-end files from folder:
  C:\Dev\myapp\dist

(node:1884) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Consider providing api-language and version using --api-language and --api-version flags,
    otherwise default values apiLanguage: node and apiVersion: 16 will apply
No deployment token found. Trying interactive login...
Checking Azure session...
Executing authenticateWithAzureIdentity
- details:
  - tenantId: <undefined>
  - clientId: <undefined>
  - clientSecret: <undefined>
 - useKeychain: true
Keychain is enabled
Executing swaCliPersistencePlugin
✔ Successfully logged into Azure!
Found an existing Azure config file, getting Tenant and Subscription Id from C:\Users\User\.azure\azureProfile.json
Selected tenant: <REDACTED>
Selected subscription: <REDACTED>
Project credentials:
 - subscriptionId: <REDACTED>
 - tenantId: <REDACTED>
 - clientId: <undefined>
 - clientSecret: <undefined>
✔ Saved project credentials in .env file.
No .gitignore file found. Skip updating .gitignore
Login successful

Checking project settings...
✖

C:\Dev\myapp>

Deploy command with --deployment-token parameter.

C:\Dev\myapp>npx swa deploy --resource-group RESGROUPNAME --app-name APPNAME --env production --deployment-token <REDACTED> --verbose=silly

Welcome to Azure Static Web Apps CLI (2.0.2)

Getting config file options from "swa-cli.config.json"...
(node:23676) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Changed directory to C:\Dev\myapp
Using configuration "myapp" from file:
  C:\Dev\myapp\swa-cli.config.json

Resolving outputLocation=dist\myapp full path...
Deploying front-end files from folder:
  C:\Dev\myapp\dist\myapp

Consider providing api-language and version using --api-language and --api-version flags,
    otherwise default values apiLanguage: node and apiVersion: 16 will apply
Deployment token provided via flag
 - --deployment-token: <hidden>

Deploying to environment: production

Trying to read workflow config with values:
 - appLocation: C:\Dev\myapp
 - outputLocation: C:\Dev\myapp\dist\myapp
 - apiLocation: <undefined>
 - dataApiLocation: <undefined>
No workflow config folder found at C:\Dev\myapp\.github\workflows
Validating user workflow config (BEFORE):
 - appLocation: C:\Dev\myapp
 - outputLocation: C:\Dev\myapp\dist\myapp
 - apiLocation: <undefined>
 - dataApiLocation: <undefined>
Validating user workflow config (AFTER):
 - appLocation: C:\Dev\myapp
 - apiLocation: <undefined>
 - outputLocation: C:\Dev\myapp\dist\myapp
 - dataApiLocation: <undefined>
Reading content from staticwebapp.config.json...
Parsing C:\Dev\myapp\staticwebapp.config.json...
Content parsed successfully

Found configuration file:
  C:\Dev\myapp\staticwebapp.config.json

Deploying project to Azure Static Web Apps...
Fetching release metadata for version: stable. Please wait...
GET https://aka.ms/swalocaldeploy
Could not find release metadata; returning undefined
✖
✖ Deployment Failed :(
✖ Deployment Failure Reason: Could not load StaticSitesClient metadata from remote. Please check your internet connection.
✖ For further information, please visit the Azure Static Web Apps documentation at https://docs.microsoft.com/azure/static-web-apps/

✖ If you believe this behavior is unexpected, please raise a GitHub issue at:
  https://github.com/Azure/static-web-apps-cli/issues/new/choose

C:\Dev\myapp>

Sample implementation of proxy support for node-fetch:

import { HttpProxyAgent } from "http-proxy-agent";
import { HttpsProxyAgent } from "https-proxy-agent";
import fetch from "node-fetch";
import { logger } from "./logger.js";

function getProxyUrl(envVar: string): string {
  const lowerCaseUrl = envVar.toLowerCase(); // Normalize case for checking
  const result = lowerCaseUrl.startsWith("http://") || lowerCaseUrl.startsWith("https://")
    ? envVar
    : `http://${envVar}`;
  logger.silly(`Using proxy: ${result}`);
  return result;
}

export function getGetProxyAgent(): HttpProxyAgent<string> | HttpsProxyAgent<string> | undefined {
  return process.env.HTTPS_PROXY
    ? new HttpsProxyAgent(getProxyUrl(process.env.HTTPS_PROXY))
    : process.env.HTTP_PROXY
        ? new HttpProxyAgent(getProxyUrl(process.env.HTTP_PROXY))
        : undefined;
}

export function fetchWithProxy(input: fetch.RequestInfo, init?: fetch.RequestInit): Promise<fetch.Response> {
  const useAgent = getGetProxyAgent();
  if (useAgent !== undefined) {
    return fetch(input, { ...init, agent: useAgent });
  } else {
    return fetch(input, init);
  }
}

Then replace

import fetch from "node-fetch";

with

import { fetchWithProxy as fetch } from "./utils/fetch-proxy.js";
@Timothyw0
Copy link
Member

Hi @DevAcc24 could you please try again with the latest v2.0.4 release? The fix has been released

@DevAcc24
Copy link
Author

@Timothyw0, with 2.0.4 seems to work as expected. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants