To create a Dask cluster from the extension. Go to the dask-labextension tab and click "+ NEW"
%% Cell type:markdown id:f32e926e tags:

%% Cell type:markdown id:5cdebde9 tags:
You can see the cluster information and have some shortcuts
* "<>" insert a cell with the necessary lines to create a Dask client. Some environment variables have been set in order to be able to use a "regular" Dask Client
* "SCALE" increase/decrease the number of workers
* "SHUTDOWN" shutdown the cluster
%% Cell type:markdown id:7c2597ed tags:

%% Cell type:code id:61fb5ed7 tags:
``` python
fromdask.distributedimportClient
client=Client("tls://192.168.100.56:45722")
client=Client("tcp://192.168.101.82:34781'")
client
```
%% Cell type:code id:5aa4058d tags:
``` python
client.cluster
```
%% Cell type:markdown id:e2e751cf tags:
The encryption of the traffic between the different Dask components is enforced through environment variables
%% Cell type:code id:909b23d9 tags:
``` python
[f'{k}={v}'fork,vinos.environ.items()if'DASK'ink]
```
%% Cell type:markdown id:b762046d tags:

%% Cell type:markdown id:86bfa20e tags:
## From python
### Using the default environment
This could be a notebook or a script submitted to HTCondor
**It is of uttermost importance to enable security when lauching a Dask cluster**, otherwise a malicious user can impersonate you by connecting to your cluster and submitting jobs to it. The attacker would then have access to your personal (e.g. SSH keys and private files in your home) and shared data.
## in jupyter.pic.es
In order to enforce security in the communication with Dask clusters, the notebooks started through jupyter.pic.es are populated with the environment variables:
See the [official documentation](https://distributed.dask.org/en/stable/tls.html#tls-ssl) to understand the function of each file.
When starting a Dask cluster through the jupyterlab extension or using the `pic_jupyterhub.dask_condor.SecureHTCondor` module in the standard environment these files will be created at runtime.
There might be issues when trying to communicate with a running Dask cluster if the security is not properly configured. See the [Troubleshooting section](#DaskSecurityTroubleshooting) for details
## in a custom cluster
When launching a cluster from a cell or a job, there are two way of providing encryption.
### Temporary security
Using temporary information stored in memory, the cluster won't be accessible from outside the process that spawned it. For this solution, the `cryptography` packages is needed. You can install it with `conda install cryptography`.
/data/jupyter/software/envs/jupyter_8440a3ce4a306902/lib/python3.12/site-packages/dask_jobqueue/core.py:745: UserWarning: Using a temporary security object without explicitly setting a shared_temp_directory: writing temp files to current working directory (/nfs/pic.es/user/t/torradeflot/services-code-samples/computing/dask) instead. You can set this value by using dask for e.g. `dask.config.set({'jobqueue.pbs.shared_temp_directory': '~'})`or by setting this value in the config file found in `~/.config/dask/jobqueue.yaml`
warnings.warn(
/data/jupyter/software/envs/jupyter_8440a3ce4a306902/lib/python3.12/site-packages/distributed/node.py:182: UserWarning: Port 8787 is already in use.
### Encryption using files generated with SecureHTCondor
In a similar you ever started a cluster using the `SecureHTCondor` module availalbe in jupyter's base environment ([how to](#Creating-a-cluster)), a set of files that can be used for SSL encryption will already be available in the folder `${HOME}/.config/dask/security` and can be used as follows.
If you try to connect a notebook to a Dask cluster, and the notebook's environment is different from the one used to launch de cluster, you may encounter compatibility issues.
You will tipycally receive a "Mismatched versions found" warning like this:
```
/data/astro/scratch2/torradeflot/envs/dask/lib/python3.11/site-packages/distributed/client.py:1388: VersionMismatchWarning: Mismatched versions found
Some mismatches might be blocking, it is recommended to match the major and minor versions. A mismatch in the patch version shouldn't be a problem.
%% Cell type:markdown id:8b2a984a tags:
## Problems with securitization
<aname="DaskSecurityTroubleshooting"></a>
## Security
If you launch a Dask cluster from a notebook but you have never launched a cluster from the dask-labextension or using the `pic_jupyterhub` module, you may encounter a problem because encryption is enforced but the certificates do not exist.
If you are trying to connect to a running Dask cluster and security is no properly configured you may encounter an error such as:
```
Cluster Start Error
Cluster
failed to start: TLS certificate does not match. Check your security
settings. More info at https://distributed.dask.org/en/latest/tls.html
```
There are different reasons why you can see this type of errors
### SSL files don't exist
If you launch a Dask cluster from a notebook in `jupyter.pic.es` but you have never launched a cluster from the dask-labextension or using the `pic_jupyterhub` module, you may encounter a problem because encryption is enforced but the certificates do not exist.
If this is the case, you can launch a cluster using one of these options as shown above. This will generate the certificate files and the subsequent creation of a Dask cluster from a notebook should succeed.
### SSL files got corrupted or expired
In some cases, the files used for the encryption can get corrupted. These files can be found in `${HOME}/.config/dask/security`. If you think this is the case, remove all the files in this folder and start a new Dask cluster from the jupyterlab extension or the `pic_jupyterhub` module in the main environment so that they can be regenerated.
### SSL configuration mismatch between client and cluster
Make sure that the encripytion configuration is consistent. Check Dask environment variables, configuration files and direct arguments used on both ends.
%% Cell type:markdown id:a28e457b tags:
# Examples
## Dask example 1
picked from https://docs.dask.org/en/stable/10-minutes-to-dask.html
# **It is of uttermost importance to enable security when lauching a Dask cluster**, otherwise a malicious user can impersonate you by connecting to your cluster and submitting jobs to it. The attacker would then have access to your personal (e.g. SSH keys and private files in your home) and shared data.
#
# ## in jupyter.pic.es
#
# In order to enforce security in the communication with Dask clusters, the notebooks started through jupyter.pic.es are populated with the environment variables:
# See the [official documentation](https://distributed.dask.org/en/stable/tls.html#tls-ssl) to understand the function of each file.
#
# When starting a Dask cluster through the jupyterlab extension or using the `pic_jupyterhub.dask_condor.SecureHTCondor` module in the standard environment these files will be created at runtime.
#
# There might be issues when trying to communicate with a running Dask cluster if the security is not properly configured. See the [Troubleshooting section](#DaskSecurityTroubleshooting) for details
#
# ## in a custom cluster
#
# When launching a cluster from a cell or a job, there are two way of providing encryption.
#
# ### Temporary security
#
# Using temporary information stored in memory, the cluster won't be accessible from outside the process that spawned it. For this solution, the `cryptography` packages is needed. You can install it with `conda install cryptography`.
# If you already have a set of CA, key, certificate files, you can use them to encrypt the traffic with and within the Dask Cluster.
# +
fromdask_jobqueueimportHTCondorCluster
fromdask.distributedimportClient
fromdistributed.securityimportSecurity
# Point this variables to the files in the file system
ca_file='/path/to/ca/file
cert_file = '/path/to/certificate/file'
key_file = '/path/to/key/file'
sec = Security(
require_encryption=True,
tls_ca_file=ca_file,
tls_client_cert=cert_file,
tls_client_key=key_file,
tls_worker_cert=cert_file,
tls_worker_key=key_file,
tls_scheduler_cert=cert_file,
tls_scheduler_key=key_file
)
secure_cluster = HTCondorCluster(
cores=1, memory='2GB', disk='10GB',
job_extra_directives={'getenv': 'True'},
security=sec)
secure_cluster.scale(1)
# -
# Then the cluster can be accessed from an independent process
# ### Encryption using files generated with SecureHTCondor
#
# In a similar you ever started a cluster using the `SecureHTCondor` module availalbe in jupyter'sbaseenvironment ([howto](#Creating-a-cluster)), a set of files that can be used for SSL encryption will already be available in the folder `${HOME}/.config/dask/security` and can be used as follows.
# Some mismatches might be blocking, it is recommended to match the major and minor versions. A mismatch in the patch version shouldn't be a problem.
# ## Problems with securitization
# <a name="DaskSecurityTroubleshooting"></a>
# ## Security
#
# If you are trying to connect to a running Dask cluster and security is no properly configured you may encounter an error such as:
#
# ```
# Cluster Start Error
# Cluster
# failed to start: TLS certificate does not match. Check your security
# settings. More info at https://distributed.dask.org/en/latest/tls.html
# ```
#
# If you launch a Dask cluster from a notebook but you have never launched a cluster from the dask-labextension or using the `pic_jupyterhub` module, you may encounter a problem because encryption is enforced but the certificates do not exist.
# There are different reasons why you can see this type of errors
#
# ### SSL files don't exist
#
# If you launch a Dask cluster from a notebook in `jupyter.pic.es` but you have never launched a cluster from the dask-labextension or using the `pic_jupyterhub` module, you may encounter a problem because encryption is enforced but the certificates do not exist.
#
# If this is the case, you can launch a cluster using one of these options as shown above. This will generate the certificate files and the subsequent creation of a Dask cluster from a notebook should succeed.
#
# ### SSL files got corrupted or expired
#
# In some cases, the files used for the encryption can get corrupted. These files can be found in `${HOME}/.config/dask/security`. If you think this is the case, remove all the files in this folder and start a new Dask cluster from the jupyterlab extension or the `pic_jupyterhub` module in the main environment so that they can be regenerated.
#
# ### SSL configuration mismatch between client and cluster
#
# Make sure that the encripytion configuration is consistent. Check Dask environment variables, configuration files and direct arguments used on both ends.