# Data libraries

Let users import input data from the buckets and folders of your company.

A data library (A folder or bucket with input data of the company. Users select files from it in the Desktop App. Platforma only reads it.) is a bucket or a folder with input data of your company, for example the output of a sequencer. Users see each library in the Desktop App (The program that each user installs on their own computer to work with Platforma. It is a client of the Backend.) and import files from it into their projects, so large data does not go through their computers. Platforma only reads a data library: the results go to the primary storage (The bucket (Amazon S3 or Google Cloud Storage) or folder where Platforma keeps all results and uploaded files.).

Each library has two names:

- the library ID (The fixed name of a data library inside Platforma. Projects keep links to files through this ID; a new ID makes a new library.): projects keep links to the imported files through it, so do not change it;
- the display name, which users see. You can change it at any time.

The Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.) reads each library in one of five access modes. Choose the mode for each library, then set it:

- [Identity of the Backend](/configuration-data-libraries/#identity-of-the-backend): one cloud identity (The identity that the cloud gives to the Platforma pod (IAM roles for service accounts on AWS, Workload Identity on Google Cloud). With it, Platforma needs no access keys.) for all.
- [Access keys](/configuration-data-libraries/#access-keys): static keys for all users.
- [Federated user access](/configuration-data-libraries/#federated-user-access): a cloud identity per user.
- [Google account of the user](/configuration-data-libraries/#google-account-of-the-user): each user’s own Google account.
- [Folder on the server or a volume](/configuration-data-libraries/#folder-on-the-server-or-a-volume): read by the Backend itself.

The Helm chart and the Terraform modules (The Terraform code that MiLaboratories provides to install Platforma. The cloud-specific Terraform modules make the cluster and its cloud parts in AWS or Google Cloud; the cloud-agnostic Terraform modules install Platforma into the cluster.) set the first two modes and the folder. The two modes with an identity for each user need Backend options. For how the modes differ for the users, the cloud and the audit log, see [Data library access modes](/security-data-library-access/). For what Platforma expects from a library, see [Data libraries](/infrastructure-contract-storage/#data-libraries). For an installation made with an installer, see [Install in cloud with installer](/install-cloud-installer/).

## Add a library

Platforma reads three kinds of library: an Amazon S3 bucket (also S3-compatible storage, for example MinIO), a Google Cloud Storage bucket, and a folder (in Kubernetes (A system that runs programs on a group of machines (a cluster). Platforma can run in a Kubernetes cluster.), a PersistentVolumeClaim). The Backend gets the list of libraries at start, so users see a new library after the Backend restarts. At start, the Backend lists each library, and it does not start when it cannot read one. The Backend does not check a library in the federated or the Google account mode at start, because it reads such a library with the identity of each user.

#### Variant: Kubernetes cluster

**Terraform modules, AWS or Google Cloud.** Two steps:

1. In `platforma-tf-cloud-base`, add the bucket to `data_libraries`. The module gives the cloud identity of the Backend read access to it. A `prefix` limits the access to one folder.

   *cloud.tfvars*

   ```hcl
   data_libraries = [
     { bucket = "my-sequencing-bucket" },
   ]
   ```

2. In `platforma-tf-services`, add the library to `backend_service.data_libraries`, with its `id`, its `name` and exactly one of `s3`, `gcs` and `pvc`. Two libraries cannot have the same ID.

   *services.tfvars*

   ```hcl
   backend_service = {
     data_libraries = [
       { id = "sequencing-archive", name = "Sequencing archive",
         s3 = { bucket = "my-sequencing-bucket", region = "eu-central-1" } },
     ]
   }
   ```

3. Run `terraform apply` in the root `cloud`, then in the root `services`, with the same values and files as at the installation (see [Install in cloud with Terraform](/install-cloud-terraform/)). The apply of the root `services` restarts the Backend by itself: the old pod stops before the new pod starts, so users cannot connect for about 20 seconds. Result: the new library is in the list of libraries in the Desktop App.

**Terraform modules, own cluster.** Do steps 2 and 3, with the apply in the root `services` only. The team that runs the cluster gives the identity of the Backend read access: see [Storage rights of the Backend](/plan-permissions-runtime/#storage).

**Helm chart.** Add the library to `dataLibraries`, then upgrade the release:

*values.yaml*

```yaml
dataLibraries:
  - id: sequencing-archive          # library ID: do not change it later
    name: "Sequencing archive"      # the name that users see
    s3:
      bucket: my-sequencing-bucket
      region: eu-central-1
```

The values of `s3` and `gcs` are the same as for the primary storage: see [Primary storage](/configuration-storage/#primary-storage). The chart does not share the file system of the Backend pod as a library.

#### Variant: Single Server

> **The host library:**
>
> When the primary storage is a folder on the server, the Backend by default also shares the file system of the server as a library. This library shows the files of the Backend itself to every user who signs in (To show the Backend who you are, with a sign-in provider or a single-user token. The verb is "sign in"; the noun and the adjective are "sign-in".). To turn it off, see [Host library](/configuration-data-libraries/#host-library).

Give one option for each library when you start the Backend. The text before `=` is the library ID:

```sh
platforma --main-root /data/platforma \
  --data-library-fs 'raw-data=/mnt/sequencer/' \
  --data-library-s3 'archive=s3://my-sequencing-bucket' \
  --data-library-s3-region 'archive=eu-central-1' \
  --data-library-name 'archive=Sequencing archive'
```

- `--data-library-fs <id>=<folder>`: a folder on the server.
- `--data-library-s3 <id>=<URL>`: `s3://<bucket>[/<prefix>]`, or `https://<endpoint>/<bucket>[/<prefix>]` for S3-compatible storage. With `--data-library-s3-region`, `--data-library-s3-external-endpoint` and `--data-library-s3-no-data-integrity`.
- `--data-library-gcs <id>=gs://<bucket>[/<prefix>]`, with `--data-library-gcs-project-id` and `--data-library-gcs-service-account`.
- `--data-library-name <id>=<name>`: the display name.

Each option also has an environment variable. The name is not always the option name: for example, `--data-library-s3` is `PL_DATA_LIBRARY_S3_URL`. See [Backend options](/reference-backend-options/).

## MiLaboratories demo data library

With the demo data library of MiLaboratories, you can do a test run when the installation is ready, without data of your own: the library holds sample sequencing data for the test projects. When the test run is green, you know that the infrastructure and the installation work.

The library is an Amazon S3 bucket of MiLaboratories. The Backend reads it with an access key that can only read this bucket. MiLaboratories publishes this key on purpose, so it is not a secret of your company:

| Setting           | Value                                      |
| ----------------- | ------------------------------------------ |
| Library ID        | `milabs-demo-data`                         |
| Bucket            | `milab-euce1-prod-eks-s3-farm-library`     |
| Region            | `eu-central-1`                             |
| Prefix            | none (the whole bucket)                    |
| Access key ID     | `AKIAXOL6R5EBCOHXV7PC`                     |
| Secret access key | `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` |

The installers add the library for you. In the AWS CloudFormation installer, it is the parameter **Enable MiLaboratories demo data library** (`EnableDemoLibrary`); in the Google Cloud installer, the question “Enable MiLaboratories demo data library?” (`ENABLE_DEMO`). Both are `true` by default and add the same bucket with the same key. On the other paths, add it as an S3 library with access keys (see [Access keys](/configuration-data-libraries/#access-keys)).

> **Access to the internet:**
>
> The Backend must reach Amazon S3 in `eu-central-1` over HTTPS. At start, the Backend lists each library and does not start when it cannot read one. Thus, do not add the library to an installation that cannot reach the internet.

#### Variant: Kubernetes cluster

First, put the key into a Secret in the namespace of Platforma:

```sh
kubectl create secret generic platforma-library-demo-creds -n <namespace> \
  --from-literal=access-key='AKIAXOL6R5EBCOHXV7PC' \
  --from-literal=secret-key='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'
```

**Terraform modules.** Add the library to `backend_service.data_libraries` of `platforma-tf-services`. Do not add the bucket to `data_libraries` of `platforma-tf-cloud-base`: the bucket is in an AWS account of MiLaboratories, and the key gives the access.

*services.tfvars*

```hcl
backend_service = {
  data_libraries = [
    { id = "milabs-demo-data",
      s3 = {
        bucket = "milab-euce1-prod-eks-s3-farm-library"
        region = "eu-central-1"
        secret = {
          access_key = { name = "platforma-library-demo-creds", key = "access-key" }
          secret_key = { name = "platforma-library-demo-creds", key = "secret-key" }
        }
      } },
  ]
}
```

**Helm chart.** Add the library to `dataLibraries`, then upgrade the release:

*values.yaml*

```yaml
dataLibraries:
  - id: milabs-demo-data
    s3:
      bucket: milab-euce1-prod-eks-s3-farm-library
      region: eu-central-1
      secret:
        accessKey: { name: platforma-library-demo-creds, key: access-key }
        secretKey: { name: platforma-library-demo-creds, key: secret-key }
```

#### Variant: Single Server

Give the library to the Backend with environment variables, for example in the environment file of the service:

```sh
PL_DATA_LIBRARY_S3_URL='milabs-demo-data=s3://milab-euce1-prod-eks-s3-farm-library'
PL_DATA_LIBRARY_S3_REGION='milabs-demo-data=eu-central-1'
PL_DATA_LIBRARY_S3_KEY='milabs-demo-data=AKIAXOL6R5EBCOHXV7PC'
PL_DATA_LIBRARY_S3_SECRET='milabs-demo-data=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'
```

The same settings are the options `--data-library-s3`, `--data-library-s3-region`, `--data-library-s3-key` and `--data-library-s3-secret`.

Users see the library with the name `milabs-demo-data` after the Backend restarts. To give it a different name, set `name` (Terraform, Helm) or `PL_DATA_LIBRARY_NAME` (Single Server mode (The mode in which the Backend and all its jobs run on one Linux machine, the server, with no Kubernetes cluster.)); do not change the ID.

## Access modes

### Identity of the Backend

The Backend reads the library with its own cloud identity. For what this means for users and the audit log, see [Identity of the Backend](/security-data-library-access/#identity-of-the-backend).

#### Variant: Kubernetes cluster

Do not give a secret in the library settings. With the cloud-specific Terraform modules (The Terraform modules platforma-tf-cloud-base and platforma-tf-cloud-cluster. There is one pair for each cloud, AWS and Google Cloud. The roots cloud and cluster apply them. They make the network, the Kubernetes cluster, the storage, the cloud identities and the registry caches. They give the hand-off file cluster_config to the cloud-agnostic Terraform modules.), the entry in `data_libraries` of `platforma-tf-cloud-base` gives the read rights (see [Add a library](/configuration-data-libraries/#add-a-library)). On an own cluster, the team that runs the cluster gives them: see [Storage rights of the Backend](/plan-permissions-runtime/#storage).

For a Google Cloud Storage library, set `gcs.service_account` (`gcs.serviceAccount`): the Google service account of the Backend, which signs the download links.

#### Variant: Single Server

Do not give keys. For Amazon S3, the Backend uses the default credentials of the AWS SDK: on a server in AWS, the IAM role of the server. For Google Cloud Storage, set `--data-library-gcs-service-account <id>=<service account>`.

### Access keys

The Backend reads the library with static keys, for a bucket that its cloud identity cannot reach. For the risks of keys that do not expire, see [Access keys](/security-data-library-access/#access-keys).

#### Variant: Kubernetes cluster

Put the keys into a Secret in the namespace of Platforma, and give its name and keys in the S3 library:

*values.yaml*

```yaml
dataLibraries:
  - id: partner-archive
    name: "Partner archive"
    s3:
      bucket: partner-bucket
      endpoint: https://s3.example.com
      secret:
        accessKey: { name: partner-archive-keys, key: access-key }
        secretKey: { name: partner-archive-keys, key: secret-key }
```

In the Terraform modules, the same values are `s3.secret.access_key` and `s3.secret.secret_key` of the library in `backend_service.data_libraries`:

*services.tfvars*

```hcl
backend_service = {
  data_libraries = [
    { id = "partner-archive", name = "Partner archive",
      s3 = {
        bucket   = "partner-bucket"
        endpoint = "https://s3.example.com"
        secret = {
          access_key = { name = "partner-archive-keys", key = "access-key" }
          secret_key = { name = "partner-archive-keys", key = "secret-key" }
        }
      } },
  ]
}
```

The Helm chart gives the keys of each library to the Backend as files from the Secret, readable by the user and the group of the Backend (mode 0440). Each S3 library can have its own keys: there is no limit on the number of libraries.

In Kubernetes, a Google Cloud Storage library cannot use a key file.

#### Variant: Single Server

Give the keys in files, one file for each key, not as values on the command line, where other users of the server can see them:

*Options of the Backend*

```sh
--data-library-s3-key-file=partner-archive=/etc/platforma/partner-archive/access-key \
--data-library-s3-secret-file=partner-archive=/etc/platforma/partner-archive/secret-key
```

Each file holds only the key, and only the system user of the Backend can read it. The Backend removes the spaces and line ends around the value. Each library can have its own pair of files.

For a Google Cloud Storage library, give the path of the key file of a service account: `--data-library-gcs-json-key-file-path <id>=<path>`. Only the system user of the Backend must be able to read this file.

### Federated user access

Each user reads the library with a cloud identity of their own, which the cloud gets from the SSO (Sign-in through the identity service of the company, with the same account as for other company systems.) token of the user. The cloud account must trust the corporate identity provider first: for what the cloud needs and what users see, see [Federated user access](/security-data-library-access/#federated-user-access). The mode needs sign-in with SSO: see [Authentication](/configuration-authentication/).

Add the library as an S3 or a Google Cloud Storage library without keys, then give these Backend options for its ID:

- **Amazon S3**:

  - `--data-library-s3-federation-role-arn <id>=<ARN>`: the IAM role that each user gets. The library also needs its region; it cannot have keys.
  - `--data-library-s3-federation-auth-token <id>=<kind>`: required with `--data-library-s3-federation-role-arn`. It selects the token of the identity provider that the Backend gives to the cloud: `id_token` or `access_token`. Google as the identity provider needs `id_token`.
  - `--data-library-s3-federation-auth-provider <id>=<provider>`: the name of the SSO provider that the role trusts. Required when there is more than one SSO provider.

- **Google Cloud Storage**:

  - `--data-library-gcs-service-account <id>=federated`;
  - `--data-library-gcs-federation-audience <id>=<audience>`: the provider of the workload identity pool, `//iam.googleapis.com/.../providers/...`;
  - `--data-library-gcs-federation-auth-token <id>=<kind>`: required with `--data-library-gcs-federation-audience`. It selects the token, as for Amazon S3;
  - `--data-library-gcs-federation-auth-provider <id>=<provider>`, as for Amazon S3;
  - optional: `--data-library-gcs-federation-token-url` and `--data-library-gcs-federation-scopes`, when the defaults of Google do not fit.

#### Variant: Kubernetes cluster

These options are not covered by the chart values or the module inputs. Add the library in `dataLibraries` (Helm) or `backend_service.data_libraries` (Terraform), and give the options in `extraArgs` or `backend_service.extra_args`:

*values.yaml*

```yaml
dataLibraries:
  - id: lab-archive
    name: "Lab archive"
    s3: { bucket: lab-archive-bucket, region: eu-central-1 }
extraArgs:
  - "--data-library-s3-federation-role-arn=lab-archive=arn:aws:iam::123456789012:role/platforma-users"
  - "--data-library-s3-federation-auth-token=lab-archive=id_token"
```

*services.tfvars*

```hcl
backend_service = {
  data_libraries = [
    { id = "lab-archive", name = "Lab archive",
      s3 = { bucket = "lab-archive-bucket", region = "eu-central-1" } },
  ]
  extra_args = [
    "--data-library-s3-federation-role-arn=lab-archive=arn:aws:iam::123456789012:role/platforma-users",
    "--data-library-s3-federation-auth-token=lab-archive=id_token",
  ]
}
```

#### Variant: Single Server

Give the options with the other options of the library, on the command line or as environment variables, for example `PL_DATA_LIBRARY_S3_FEDERATION_ROLE_ARN`.

**A library that users already use.** With federated access, imports copy the files into the primary storage. Files that users imported before, in another mode, stay links to the library. Before you change a library to federated access, copy these files into the primary storage: see [Change the access mode of a data library](/maintain-data-library-access-mode/).

### Google account of the user

Each user reads a Google Cloud Storage library with their own Google account. For the scopes of the token and what users see, see [Google account of the user](/security-data-library-access/#google-account-of-the-user). Users must sign in with SSO through Google, and the sign-in must ask for access to Cloud Storage and for a refresh token: see [Authentication](/configuration-authentication/).

Set these values for the library:

- the service account `authenticated-user`: `--data-library-gcs-service-account <id>=authenticated-user`;
- `--data-library-gcs-auth-provider <id>=<provider>`: the name of the Google SSO provider.

#### Variant: Kubernetes cluster

The provider option is not covered by the chart values or the module inputs. Set `serviceAccount: authenticated-user` in the library, and give the provider in `extraArgs` (`backend_service.extra_args` in Terraform):

*values.yaml*

```yaml
dataLibraries:
  - id: shared-references
    name: "Shared references"
    gcs: { bucket: shared-references-bucket, projectID: my-project, serviceAccount: authenticated-user }
extraArgs:
  - "--data-library-gcs-auth-provider=shared-references=google"
```

*services.tfvars*

```hcl
backend_service = {
  data_libraries = [
    { id = "shared-references", name = "Shared references",
      gcs = { bucket = "shared-references-bucket", project_id = "my-project", service_account = "authenticated-user" } },
  ]
  extra_args = ["--data-library-gcs-auth-provider=shared-references=google"]
}
```

#### Variant: Single Server

Give the two options with the other options of the library, or the environment variables `PL_DATA_LIBRARY_GCS_SERVICE_ACCOUNT` and `PL_DATA_LIBRARY_GCS_AUTH_PROVIDER`.

### Folder on the server or a volume

The Backend reads the files itself, with its own system user. For what this means for users, see [Folder on the server or a volume](/security-data-library-access/#folder-on-the-server-or-a-volume).

#### Variant: Kubernetes cluster

Create the PersistentVolumeClaim in the namespace of Platforma first, then give it in `pvc` of the library. The chart mounts it read-only into the Backend pod only. A sub-path (`subPath`, `sub_path`) uses one folder of the volume.

*values.yaml*

```yaml
dataLibraries:
  - id: shared-folder
    name: "Shared folder"
    pvc:
      existingClaim: shared-data
```

*services.tfvars*

```hcl
backend_service = {
  data_libraries = [
    { id = "shared-folder", name = "Shared folder",
      pvc = { existing_claim = "shared-data" } },
  ]
}
```

The files must be readable by the user and the group of the Backend pod (1010 by default).

#### Variant: Single Server

Give `--data-library-fs <id>=<folder>`. The files must be readable by the system user of the Backend. For the folder that the Backend shares by default, see [Host library](/configuration-data-libraries/#host-library).

### Host library

The host library exists only in Single Server mode, and only when the primary storage is a folder on the server. Then the Backend by default shares the file system of the server with all users as a read-only data library with the ID `host`. Users can then import any file that the system user of the Backend can read, with no copy into a library folder. With a bucket as the primary storage, and in Kubernetes, there is no host library.

MiLaboratories recommends to turn the host library off: it shows the files of the server to every user who signs in, also the files of the Backend itself. To turn it off, give `--no-host-data-library` (`PL_NO_HOST_DATA_LIBRARY=true`), and give the folders that users need as [folder libraries](/configuration-data-libraries/#folder-on-the-server-or-a-volume).

## Change or remove a library

A project keeps links to the files that users imported from a library in the modes Identity of the Backend, Access keys and Folder on the server or a volume, not copies. Thus some changes of a library change what projects can read. To add a library and to change its display name is safe. A new library ID makes a new library: the Backend keeps the old ID as a removed library, and the imported files stay linked to it.

- To move a library to a per-user mode, copy its imported files into the primary storage first: see [Change the access mode of a data library](/maintain-data-library-access-mode/).
- To remove a library, choose what happens to the projects that use it: see [Remove a data library](/maintain-removing-data-library/). The Backend checks a removed library at each start too, and it does not start when it cannot list it.

## Problems

### Users do not see the library

**Cause:** the Backend did not get the new settings. **Fix:** make sure that the Backend restarted after the change. When the Backend cannot read the library, it does not start: see the next problem.

### The Backend does not start after a change of a library

**Cause:** the Backend cannot list a library that is in its settings: the bucket, its access rights or its keys changed. The log of the Backend says `Cannot list storage` with the library ID. **Fix:** give the Backend read access to the library again. This is also necessary for a library that you removed from the settings: the Backend checks it at each start too. See [Remove a data library](/maintain-removing-data-library/).

### Users see “access denied” for some files

**Cause:** in the federated and the Google account modes, the cloud decides what each user can read. **Fix:** none is necessary when this is the intended rule; else change the rights of the user in the cloud.

## Limits

- Platforma cannot write to a data library.
- In Kubernetes, the federated and the Google account modes are set with Backend options in `extraArgs`: the Helm chart does not check them.
- In Kubernetes, a Google Cloud Storage library cannot use a key file.

## Next

- [Data library access modes](/security-data-library-access/)
- [Storage](/configuration-storage/)

Source: https://docs.platforma.bio/configuration-data-libraries/
