|
1 | 1 | # Azure DevOps Proxy
|
2 |
| -Proxy application to allow one PAT to be shared with limited scope to other clients. |
| 2 | +[](https://goreportcard.com/report/github.com/XenitAB/azdo-proxy) |
| 3 | +[](https://quay.io/repository/xenitab/azdo-proxy) |
| 4 | + |
| 5 | +Proxy to allow controlled sharing of a Azure DevOps Personal Access Token. |
| 6 | + |
| 7 | +Azure DevOps allows the use of Personal Access Tokens (PAT) to authenticate access to both its |
| 8 | +API and Git repositories. Sadly it does not provide an API to create new PAT, making the process |
| 9 | +of automation cumbersome if multiple tokens are needed with limited scopes. |
| 10 | + |
| 11 | +<p align="center"> |
| 12 | + <img src="./assets/architecture.png"> |
| 13 | +</p> |
| 14 | + |
| 15 | +Azure Devops Proxy (azdo-proxy) is an attempt to solve this issue by enabling a single PAT |
| 16 | +to be shared by many applications, while at the same time limiting access for each application. |
| 17 | +Requests are sent to azdo-proxy together with a token, which gives access to a specific repository. |
| 18 | +The request is checked and if allowed forwarded to Azure DevOps with the PAT appended to the request. |
| 19 | + |
| 20 | +## How To |
| 21 | +Start off by [creating a new PAT](https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page) as it has to be given to the proxy. |
| 22 | + |
| 23 | +> The example will show how to run azdo-proxy in Kubernetes, but there is nothing limiting azdo-proxy to run in any other environment. |
| 24 | +
|
| 25 | +The proxy reads its configuration from a JSON file. The file will contain the PAT used to authenticate requests with, the Azure DevOps organization, and a list of repositories that can be accessed through the proxy along with a unique token for each repository. |
| 26 | +```json |
| 27 | +{ |
| 28 | + "pat": "<pat>", |
| 29 | + "organization": "org", |
| 30 | + "repositories": [ |
| 31 | + { |
| 32 | + "project": "project", |
| 33 | + "name": "repo-1", |
| 34 | + "token": "<token-1>" |
| 35 | + }, |
| 36 | + { |
| 37 | + "project": "project", |
| 38 | + "name": "repo-2", |
| 39 | + "token": "<token-2>" |
| 40 | + } |
| 41 | + ] |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +Create a Kubernetes secret containing the configuration JSON file. |
| 46 | +```shell |
| 47 | +kubectl create secret generic azdo-proxy-config --from-file=config.json |
| 48 | +``` |
| 49 | + |
| 50 | +Add the Helm repository and install the chart, be sure to set the secret name. |
| 51 | +```shell |
| 52 | +helm repo add https://xenitab.github.io/azdo-proxy/ |
| 53 | +helm install azdo-proxy --set configSecretName=azdo-proxy-config |
| 54 | +``` |
| 55 | + |
| 56 | +There should now be a azdo-proxy Pod and Service in the cluster, ready to proxy traffic. |
| 57 | + |
| 58 | +### GIT |
| 59 | +Cloning a repository through the proxy is not too different from doing so directly from Azure DevOps. |
| 60 | +The only limitation is that it is not possible to clone through ssh, as azdo-proxy only proxies http traffic. |
| 61 | +To clone the repository `repo-1` [get the clone url from the respository page](https://docs.microsoft.com/en-us/azure/devops/repos/git/clone?view=azure-devops&tabs=visual-studio#get-the-clone-url-to-your-repo). |
| 62 | +Then replace the host part of the url with `azdo-proxy` and att the token as a basci auth parameter. |
| 63 | +The result should be similar to below. |
| 64 | +```shell |
| 65 | +git clone http://<token-1>@azdo-proxy/org/proj/_git/repo-1 |
| 66 | +``` |
| 67 | + |
| 68 | +### API |
| 69 | +Authenticated API calls can also be done through the proxy. Currently only repository specific |
| 70 | +requests will be permitted. This may change in future releases. As an example execute the |
| 71 | +following command to list all pull requests in the repository `repo-1`. |
| 72 | +```shell |
| 73 | +curl http://<token-1>@azdo-proxy/org/proj/_apis/git/repositories/repo-1/pullrequests?api-version=5.1 |
| 74 | +``` |
| 75 | + |
| 76 | +> :warning: **If you intend on using a language specific API**: Please read this! |
| 77 | +
|
| 78 | +Some APIs built by Microsoft, like [azure-devops-go-api](https://github.com/microsoft/azure-devops-go-api), will make a request to the [Resource Areas API](https://docs.microsoft.com/en-us/azure/devops/extend/develop/work-with-urls?view=azure-devops&tabs=http#how-to-get-an-organizations-url) |
| 79 | +which returns a list of location URLs for a specific organization. They will then use those URLs |
| 80 | +when making additional requests, skipping the proxy. To avoid this you need to explicitly create |
| 81 | +your client instead of allowing it to be created automatically. |
| 82 | + |
| 83 | +In the case of Go you should create a client in the following way. |
| 84 | +```golang |
| 85 | +package main |
| 86 | + |
| 87 | +import ( |
| 88 | + "github.com/microsoft/azure-devops-go-api/azuredevops" |
| 89 | + "github.com/microsoft/azure-devops-go-api/azuredevops/git" |
| 90 | +) |
| 91 | + |
| 92 | +func main() { |
| 93 | + connection := azuredevops.NewAnonymousConnection("http://azdo-proxy") |
| 94 | + client := connection.GetClientByUrl("http://azdo-proxy") |
| 95 | + gitClient := &git.ClientImpl{ |
| 96 | + Client: *client, |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +Instead of the cleaner solution which would ignore the proxy. |
| 102 | +```golang |
| 103 | +package main |
| 104 | + |
| 105 | +import ( |
| 106 | + "context" |
| 107 | + |
| 108 | + "github.com/microsoft/azure-devops-go-api/azuredevops" |
| 109 | + "github.com/microsoft/azure-devops-go-api/azuredevops/git" |
| 110 | +) |
| 111 | + |
| 112 | +func main() { |
| 113 | + connection := azuredevops.NewAnonymousConnection("http://azdo-proxy") |
| 114 | + ctx := context.Background() |
| 115 | + gitClient, _ := git.NewClient(ctx, connection) |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## License |
| 120 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments