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

Support http proxies #15

Closed
2 tasks done
Tracked by #1
baywet opened this issue Nov 4, 2021 · 6 comments · Fixed by microsoft/kiota-http-go#32
Closed
2 tasks done
Tracked by #1

Support http proxies #15

baywet opened this issue Nov 4, 2021 · 6 comments · Fixed by microsoft/kiota-http-go#32
Assignees
Labels
enhancement New feature or request Standard GitHub label Issue caused by core project dependency modules or library

Comments

@baywet
Copy link
Member

baywet commented Nov 4, 2021

Customers must be able to configure an HTTP proxy for the request adapter so the applications they build can run in networks that require accessing microsoft graph through a proxy.
Docs should also be updated once this is implemented

Tasks

@baywet baywet mentioned this issue Nov 4, 2021
8 tasks
@baywet baywet added the enhancement New feature or request label Nov 4, 2021
@baywet baywet added this to the Core specification alignment milestone Nov 4, 2021
@baywet
Copy link
Member Author

baywet commented Nov 4, 2021

this should probably live in kiota http if any code change is required, related microsoft/kiota#371

@baywet baywet added the Standard GitHub label Issue caused by core project dependency modules or library label Nov 8, 2021
@baywet baywet mentioned this issue Mar 17, 2022
25 tasks
@rkodev rkodev self-assigned this Aug 15, 2022
@rkodev
Copy link
Contributor

rkodev commented Sep 14, 2022

@baywet We might need to introduce a method on the abstractions.RequestAdapter known as UsingProxie(proxy string) so as the users can set this on the adapter. i.e

One approach can be

adapter, err := NewNetHttpRequestAdapter(authProvider)
adapter.UsingProxy("")
// or 
adapter.WithProxie("")

@baywet
Copy link
Member Author

baywet commented Sep 14, 2022

is this something that is set on the net/http client or on the request?

@rkodev
Copy link
Contributor

rkodev commented Sep 14, 2022

This should be in the client itself

//creating the proxyURL
	proxyStr := "http://localhost:7000"
	proxyURL, err := url.Parse(proxyStr)
	if err != nil {
		log.Println(err)
	}

	//creating the URL to be loaded through the proxy
	urlStr := "http://httpbin.org/get"
	url, err := url.Parse(urlStr)
	if err != nil {
		log.Println(err)
	}

	//adding the proxy settings to the Transport object
	transport := &http.Transport{
		Proxy: http.ProxyURL(proxyURL),
	}

	//adding the Transport object to the http Client
	client := &http.Client{
		Transport: transport,
	}

@baywet
Copy link
Member Author

baywet commented Sep 14, 2022

it's a lot of boiler plate code, and we're already using the transport for our middleware. Could we just improve the http client factory to take into account the proxy settings? like GetDefaultClientWithProxySettings
Also how do you set proxy authentication (when needed) ?

@rkodev
Copy link
Contributor

rkodev commented Sep 14, 2022

Added 2 factories to allow for proxy communication with and without authentication

// GetDefaultClientWithProxySettings creates a new default net/http client with a proxy url and default middleware
func GetDefaultClientWithProxySettings(proxyUrlStr string) *nethttp.Client {
	client := getDefaultClientWithoutMiddleware()
	client.Transport = getTransportWithProxy(proxyUrlStr, nil)
	return client
}

// GetDefaultClientWithAuthenticatedProxySettings creates a new default net/http client with a proxy url and default middleware
func GetDefaultClientWithAuthenticatedProxySettings(proxyUrlStr string, username string, password string) *nethttp.Client {
	client := getDefaultClientWithoutMiddleware()

	user := url.UserPassword(username, password)
	client.Transport = getTransportWithProxy(proxyUrlStr, user)
	return client
}

func getTransportWithProxy(proxyUrlStr string, user *url.Userinfo) nethttp.RoundTripper {
	proxyURL, err := url.Parse(proxyUrlStr)
	if err != nil {
		log.Println(err)
	}

	if user != nil {
		proxyURL.User = user
	}

	transport := &nethttp.Transport{
		Proxy: nethttp.ProxyURL(proxyURL),
	}

	middlewares := GetDefaultMiddlewares()

	return NewCustomTransportWithParentTransport(transport, middlewares...)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Standard GitHub label Issue caused by core project dependency modules or library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants