# IAM ACV OIDC MIGRATION GUIDE

Back offices that are behind IAM ACV should migrate to OIDC, IAM headers are now deprecated.

## ADMIN

- You have to request a new VIP for your application (<env>-<deployunit>.etat-de-vaud.ch)
    - i.e. for sesamebo (R260429_00018, R260810_00031 for eldoradobo prod) : https://int-sesame.etat-de-vaud.ch/
    - Pay attention that they won't be able to finish the task if no pod has been started, so you need  
      to synchronize the guys, especially for production
- On IAM side, you have to request a client id / secret on every environment
    - i.e. for sesamebo (S260429_00110)
    - PUT it in your configuration before releasing, i.e. https://dsigit.etat-de-vaud.ch/outils/git/projects/CYBER/repos/configuration-bo/browse/eldoradobo-PR.properties#28

## FRONTEND

- Enable the interceptor that is used for dev server by providing the following injection token in your app.component.ts : `{ provide: OIDC_ENABLED, useValue: 'true' }`
- Update your proxy.conf.json to add `/oauth2`, `/login`, `/logout` :

```json
[
    {
        "context": ["/api", "/oauth2", "/login", "/logout"],
        "target": "http://localhost:20900",
        "secure": false
    }
]
```

- update your angular.json

```json
  "baseHref": "/",
```

## BACKEND

- remove the server.servlet.context-path (your application should respond on "/")
- Replace this import : `cyberadminbe.config.CyberAdminBeSecurityConfig` with this one `backofficebe.config.IamAcvOidcSecurityConfiguration`
- add the following dependency :

```xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
```

- update your application.properties with

```properties
# OIDC common properties
spring.security.oauth2.client.registration.acv.client-name=SSO ACV
spring.security.oauth2.client.registration.acv.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.acv.scope=openid,profile,email
```

- update your CO/environment.properties + UT/environment.properties with

```properties
spring.security.oauth2.client.registration.acv.client-id=mock-client-id
spring.security.oauth2.client.registration.acv.client-secret=anySecret
spring.security.oauth2.client.provider.acv.issuer-uri=http://localhost:8080/acv
```

- Expose the actuators on a different port, by updating your application.properties

```properties
# With OIDC we have to expose actuators without any security to be called by spring boot admin
# so the are exposed on another port
management.server.port=${port.divers:8086}
# expose liveness/readiness probes on the same port as the application (/livez / readyz)
management.endpoint.health.probes.add-additional-paths=true
# send actuator access log to /dev/stdout
management.server.tomcat.accesslog.enabled=true
management.server.tomcat.accesslog.directory=/dev
management.server.tomcat.accesslog.prefix=stdout
management.server.tomcat.accesslog.suffix=
```

### TESTING

To be able to use @SpringBootTest, you have to :

- add the following dependency :

```xml
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
```

- provide the token in your mock request, you can replace this helper :

```java
    static MockHttpServletRequestBuilder enrichWithAuthHeaders(MockHttpServletRequestBuilder builder) {
        return builder.header("iam-userid", "sar8h6")
                .header("iam-firstname", "Bruce")
                .header("iam-lastname", "Wayne")
                .header("iam-application", "app")
                .header("iam-roles", "app-user");
    }
```

with methods from `ch.vd.cyber.backofficebe.utils.MVCTestUtils`:

- `enrichWithOidcTokenAsUser` : simulates that a request is done by an IAM user, with a valid OIDC token and the role "user"
- `enrichWithOidcTokenAsRole` : same as previous, but you can set another role than "user"
- `enrichWithAuthHeadersForEsgateAsUser` : simulates that a request is done by a techincal user through esgate and the role "user"
- `enrichWithAuthHeadersForEsgate` : same as previous, but you can set another role than "user"

## RUNNING YOUR APP

- You can run a mock OIDC server with this command :

```shell
podman login docker-registry.etat-de-vaud.ch
```

```shell
podman run -p 8080:8080 --rm docker-registry.etat-de-vaud.ch/tools/mock-oidc:latest
```

- You can also run your app with the mock OIDC server on startup by adding the following line in your TomcatRunner.java

```java
TomcatRunnerHelper.startIamAcvOIDCMockServer();
```

## Troubleshooting

```shell
Error: writing blob: adding layer with blob "sha256:c0980b751eb29a6c18ccca8f03a091a556268d924d2021c32c94db254c54d304": processing tar file(lchown /home/java: value too large for defined data type): exit status 1
```

If you have this error when running the mock OIDC server, you need to raise the UID/GID range.

- Edit `/etc/subuid` and `/etc/subgid` and change the range for your user (i.e. `youruser:10000:65536` to `youruser:1000000:1000000`)
- Execute `podman system migrate` to apply the changes
- Try to run the mock OIDC server again
