Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ The journey can start [here](WALKTHROUGH.md) for a quickstart with a global over
```shell
.
├── api-gateway # Traefik Hub API Gateway tutorials
│   ├── 1-getting-started
│   ├── 2-secure-applications
│   ├── 1-getting-started # API Gateway Quick Start Guide
│   ├── 2-expose
│   ├── 3-secure-applications
├── api-management # Traefik Hub API Management tutorials
│   ├── 1-getting-started
│   ├── 2-access-control
│   ├── 3-api-lifecycle-management
│   └── 4-protect-api-infrastructure (WIP)
│   ├── 1-getting-started # API Management Quick Start Guide
└── src
   ├── api-server # API server source code
   └── manifests # Yaml to deploy all apps
Expand Down
234 changes: 1 addition & 233 deletions WALKTHROUGH.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,236 +391,4 @@ We'll need Traefik Hub with API Management!

## Step 3: Manage an API with Traefik Hub API Management

First, we enable API Management on Traefik Traefik Hub using the same Helm chart:

```shell
helm upgrade traefik -n traefik --wait \
--version v34.4.1 \
--reuse-values \
--set hub.apimanagement.enabled=true \
traefik/traefik
```

Traefik Hub API Management is 100% compatible with Traefik Proxy v3 and Traefik Hub API Gateway.

The dashboard is still reachable on http://dashboard.docker.localhost/

![Local Traefik Hub Dashboard](./src/images/hub-dashboard.png)

And also confirm that the API is still secured using an API Key:

```shell
# This call is not authorized => 401
curl -i http://walkthrough.docker.localhost/api-key/weather
# This call with the token is allowed => 200
curl -s -H "Authorization: Bearer $API_KEY" http://walkthrough.docker.localhost/api-key/weather | jq
```

Now, let's try to manage it with Traefik Hub using `API` and `APIAccess` resources:

```yaml :src/manifests/walkthrough/api.yaml -s 1 -e 23
---
apiVersion: hub.traefik.io/v1alpha1
kind: API
metadata:
name: walkthrough-weather-api
namespace: apps
spec:
openApiSpec:
path: /openapi.yaml
override:
servers:
- url: http://api.walkthrough.docker.localhost

---
apiVersion: hub.traefik.io/v1alpha1
kind: APIAccess
metadata:
name: walkthrough-weather-api
namespace: apps
spec:
apis:
- name: walkthrough-weather-api
everyone: true
```

We'll need to reference this API in the `IngressRoute` with an annotation:

```yaml :src/manifests/walkthrough/api.yaml -s 25 -e 41
---
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: walkthrough-weather-api
namespace: apps
annotations:
hub.traefik.io/api: walkthrough-weather-api # <=== Link to the API using its name
spec:
entryPoints:
- web
routes:
- match: Host(`api.walkthrough.docker.localhost`) && PathPrefix(`/weather`)
kind: Rule
services:
- name: weather-app
port: 3000
```

:information_source: We've also removed the API Key authentication middleware, as we'll use Traefik Hub's built-in identity provider for user and credential management. The API is still secured, as we'll see it shortly.

Let's apply it:

```shell
kubectl apply -f src/manifests/walkthrough/api.yaml
```

It will create `API`, `APIAccess` and link `IngressRoute` to this API.

```shell
api.hub.traefik.io/walkthrough-weather-api created
apiaccess.hub.traefik.io/walkthrough-weather-api created
ingressroute.traefik.io/walkthrough-weather-api created
```

Now, we can confirm this API is not publicly exposed:

```shell
curl -i http://api.walkthrough.docker.localhost/weather
```

It returns the expected 401 Unauthorized HTTP code:

```shell
HTTP/1.1 401 Unauthorized
Date: Mon, 06 May 2024 12:09:56 GMT
Content-Length: 0
```

## Step 4: Create a user for this API

Users are created in the [Traefik Hub Online Dashboard](https://hub.traefik.io/users):

![Create user admin](./api-management/1-getting-started/images/create-user-admin.png)

## Step 5: Deploy the API Portal

The user created previously will connect to an API Portal to generate an API key, so let's deploy the API Portal!

```yaml :src/manifests/walkthrough/api-portal.yaml
---
apiVersion: hub.traefik.io/v1alpha1
kind: APIPortal
metadata:
name: walkthrough-apiportal
namespace: apps
spec:
title: API Portal
description: "Apps Developer Portal"
trustedUrls:
- http://api.walkthrough.docker.localhost

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: walkthrough-apiportal
namespace: traefik
annotations:
# This annotation link this Ingress to the API Portal using <name>@<namespace> format.
hub.traefik.io/api-portal: walkthrough-apiportal@apps
spec:
rules:
- host: api.walkthrough.docker.localhost
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: apiportal
port:
number: 9903
```

:information_source: This API Portal is routed with the internal _ClusterIP_ `Service` named apiportal.

```shell
kubectl apply -f src/manifests/walkthrough/api-portal.yaml
sleep 60
```

```shell
apiportal.hub.traefik.io/walkthrough-apiportal created
ingress.networking.k8s.io/walkthrough-apiportal created
```

The API Portal should be reachable on http://api.walkthrough.docker.localhost

We log in with the admin user.

![API Portal Log in](./api-management/1-getting-started/images/api-portal-login.png)

And create a token for this user:

![API Portal Create Token](./api-management/1-getting-started/images/api-portal-create-token.png)

```shell
export ADMIN_TOKEN="XXX"
```

Request the API with this token: :tada:

```shell
curl -s -H "Authorization: Bearer $ADMIN_TOKEN" http://api.walkthrough.docker.localhost/weather | jq
```

```json
[
{"city":"GopherTown","id":"0","weather":"Cloudy"},
{"city":"City of Gophers","id":"1","weather":"Sunny"},
{"city":"GopherRocks","id":"2","weather":"Cloudy"}
]
```

:information_source: If it fails with 401, wait one minute and try again. The token needs to be sync before it can be accepted by Traefik Hub.

We can see the API available in the `apps` namespace in the portal. We advise every API to come with an OpenAPI specification (OAS):

![API Portal with OAS](./api-management/1-getting-started/images/api-portal-with-oas.png)

However, it's still possible not setting an OAS, but it severely hurts getting started with API consumption.

This time, we won't specify any OAS in the API _CRD_:

```yaml :src/manifests/walkthrough/forecast.yaml -s 1 -e 7
---
apiVersion: hub.traefik.io/v1alpha1
kind: API
metadata:
name: walkthrough-weather-api-forecast
namespace: apps
spec: {}
```

The other resources are built on the same model, as we can see in [the complete file](https://github.com/traefik/hub/blob/main/api-management/1-getting-started/manifests/forecast.yaml). Let's apply it:

```shell
kubectl apply -f src/manifests/weather-app-forecast.yaml
kubectl apply -f src/manifests/walkthrough/forecast.yaml
```

```shell
api.hub.traefik.io/walkthrough-weather-api-forecast created
apiaccess.hub.traefik.io/walkthrough-weather-api-forecast created
ingressroute.traefik.io/walkthrough-weather-api-forecast created
```

Request the API with the token:

```shell
curl -H "Authorization: Bearer $ADMIN_TOKEN" http://api.walkthrough.docker.localhost/forecast/weather
```

And that's it! This time, we have documentation built from the OpenAPI specification, and we can also interactively try the API with the Try Out functionality.

![API Portal without OAS](./api-management/1-getting-started/images/api-portal-without-oas.png)
This section is coming soon. In the meantime, you can follow the [Traefik Hub API Management quick start guide](https://doc.traefik.io/traefik-hub/api-management/quick-start-guide) tutorial.
Loading