gitlab-project-deploy-key-authorized

Description

Add a deploy key to a GitLab project.

This can be used to authorize your (or a host-) ssh public key to be able to pull from private repositories, or push a public or private repository. Useful for CI pipelines and such.

Variables

Name Type Default Description

access_token

string --

The Gitlab access token. Required

gitlab_project

string --

The name of the Gitlab project. Required

gitlab_user

string --

The Gitlab user- or organization name. Required

key_content

string --

The content of the public ssh key. Required

path_to_key

string --

The path to the private ssh key.

The path to the public key will be infered (added '.pub'). Required

bits

integer 4096

The number of bits in the key to create.

can_push

boolean False

Whether the key in question is allowed to push to the repo.

deploy_key_title

string deploy

The title of the deploy key on Gitlab.

password

string --

The password to unlock the key (only used if key doesn't exist already).

path

string ~/.ssh/id_ed25519

The absolute path to the private ssh key. The key type token (e.g. rsa) will be added.

The path to the public key will be infered (added '.pub').

user

string --

The name of the ssh key owner.

Code

doc:
  short_help: Add a deploy key to a GitLab project.
  help: |
    Add a deploy key to a GitLab project.

    This can be used to authorize your (or a host-) ssh public key to be able to pull from private repositories, or push a public or private repository. Useful for CI pipelines and such.

args:
  _import: ssh-key-exists
  key_content:
    doc:
      short_help: The content of the public ssh key.
    type: string
    required: true
    excludes: path
  path_to_key:
    doc:
      short_help: The path to the private ssh key.
      help: |
        The path to the private ssh key.

        The path to the public key will be infered (added '.pub').
    type: string
    required: true
    excludes: key_content
  access_token:
    doc:
      short_help: The Gitlab access token.
    type: string
    required: true
    secret: true
  gitlab_user:
    doc:
      short_help: The Gitlab user- or organization name.
    type: string
    required: true
  gitlab_project:
    doc:
      short_help: The name of the Gitlab project.
    type: string
    required: true
  deploy_key_title:
    doc:
      short_help: The title of the deploy key on Gitlab.
    type: string
    required: false
    default: deploy
  can_push:
    doc:
      short_help: Whether the key in question is allowed to push to the repo.
    type: boolean
    default: false
    cli:
      param_decls:
      - --can-push

frecklets:
- ssh-key-exists:
#      frecklet::skip: "{{:: path_to_key | true_if_empty ::}}"
    path: '{{:: path_to_key ::}}'
    password: '{{:: password ::}}'
    user: '{{:: user ::}}'
    key_type: rsa
    bits: '{{:: bits ::}}'
- frecklet:
    name: add-gitlab-deploy-key.at.yml
    type: ansible-tasklist
    properties:
      elevated: false
      internet: true
      idempotent: true
    desc:
      short: "add deploy key with alias '{{:: deploy_key_title ::}}' to GitLab project\
        \ '{{:: gitlab_user ::}}/{{:: gitlab_project ::}}'"
      long: |
        {%:: if path -::%}
        Read the public key file '{{:: path_to_key ::}}.pub' and use the the content to create or overwrite a trusted deploy key for the specified GitLab project, using the following details:{%:: else -::%}
        Use the 'key_content' string ( {{:: key_content ::}} ) to create or overwrite a trusted deploy key for the specifieed GitLab project, using the following details:{%:: endif ::%}

            gitlab access token: <your secret token>
            gitlab user/organization: {{:: gitlab_user ::}}
            gitlab project: {{:: gitlab_project ::}}
            deploy key alias: {{:: deploy_key_title ::}}
            'push' allowed for key: {{:: can_push ::}}
      references:
        GitLab deploy key documentation: https://docs.gitlab.com/ee/ssh/
    resources:
      python-package:
      - python-gitlab
      ansible-tasklist:
      - add-gitlab-deploy-key.at.yml
      - internally-register-public-ssh-key.at.yml
  task:
    become: false
  vars:
    __path_to_key__: '{{:: path_to_key ::}}'
    __key_content__: '{{:: key_content ::}}'
    __project_name__: '{{:: gitlab_user ::}}/{{:: gitlab_project ::}}'
    __gitlab_private_access_token__: '{{:: access_token ::}}'
    __deploy_key_title__: '{{:: deploy_key_title ::}}'
    __can_push__: '{{:: can_push ::}}'
    __use_become__: '{{:: user | false_if_empty ::}}'
frecklecute gitlab-project-deploy-key-authorized --help

Usage: frecklecute gitlab-project-deploy-key-authorized [OPTIONS]

  Add a deploy key to a GitLab project.

  This can be used to authorize your (or a host-) ssh public key to be able
  to pull from private repositories, or push a public or private repository.
  Useful for CI pipelines and such.

Options:
  --access-token ACCESS_TOKEN     The Gitlab access token.  [required]
  --gitlab-project GITLAB_PROJECT
                                  The name of the Gitlab project.  [required]
  --gitlab-user GITLAB_USER       The Gitlab user- or organization name.
                                  [required]
  -b, --bits BITS                 The number of bits in the key to create.
  --can-push                      Whether the key in question is allowed to
                                  push to the repo.
  --deploy-key-title DEPLOY_KEY_TITLE
                                  The title of the deploy key on Gitlab.
  --key-content KEY_CONTENT       The content of the public ssh key.
  --password PASSWORD             The password to unlock the key (only used if
                                  key doesn't exist already).
  --path PATH                     The (absolute!) path (without key_type
                                  token) to the (private) ssh key.
  --path-to-key PATH_TO_KEY       The path to the private ssh key.
  --user USER                     The name of the ssh key owner.
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.gitlab_project_deploy_key_authorized.GitlabProjectDeployKeyAuthorized
#


from dataclasses import dataclass
from pyckles import AutoPycklet
from typing import *    # noqa

@dataclass
class GitlabProjectDeployKeyAuthorized(AutoPycklet):
    """Add a deploy key to a GitLab project.

     This can be used to authorize your (or a host-) ssh public key to be able to pull from private repositories, or push a public or private repository. Useful for CI pipelines and such.

       Args:
         access_token: The Gitlab access token.
         bits: The number of bits in the key to create.
         can_push: Whether the key in question is allowed to push to the repo.
         deploy_key_title: The title of the deploy key on Gitlab.
         gitlab_project: The name of the Gitlab project.
         gitlab_user: The Gitlab user- or organization name.
         key_content: The content of the public ssh key.
         password: The password to unlock the key (only used if key doesn't exist already).
         path: The (absolute!) path (without key_type token) to the (private) ssh key.
         path_to_key: The path to the private ssh key.
         user: The name of the ssh key owner.

    """

    FRECKLET_ID = "gitlab-project-deploy-key-authorized"

    access_token: str = None
    bits: int = None
    can_push: bool = None
    deploy_key_title: str = None
    gitlab_project: str = None
    gitlab_user: str = None
    key_content: str = None
    password: str = None
    path: str = None
    path_to_key: str = None
    user: str = None


    def __post_init__(self):
        super(GitlabProjectDeployKeyAuthorized, self).__init__(var_names=["access_token", "bits", "can_push", "deploy_key_title", "gitlab_project", "gitlab_user", "key_content", "password", "path", "path_to_key", "user"])


frecklet_class = GitlabProjectDeployKeyAuthorized
# -*- coding: utf-8 -*-


#
# module path: pycklets.gitlab_project_deploy_key_authorized.GitlabProjectDeployKeyAuthorized
#


from pyckles import AutoPycklet

class GitlabProjectDeployKeyAuthorized(AutoPycklet):
    """Add a deploy key to a GitLab project.

     This can be used to authorize your (or a host-) ssh public key to be able to pull from private repositories, or push a public or private repository. Useful for CI pipelines and such.

       Args:
         access_token: The Gitlab access token.
         bits: The number of bits in the key to create.
         can_push: Whether the key in question is allowed to push to the repo.
         deploy_key_title: The title of the deploy key on Gitlab.
         gitlab_project: The name of the Gitlab project.
         gitlab_user: The Gitlab user- or organization name.
         key_content: The content of the public ssh key.
         password: The password to unlock the key (only used if key doesn't exist already).
         path: The (absolute!) path (without key_type token) to the (private) ssh key.
         path_to_key: The path to the private ssh key.
         user: The name of the ssh key owner.

    """

    FRECKLET_ID = "gitlab-project-deploy-key-authorized"

    def __init__(self, access_token=None, bits=4096, can_push=None, deploy_key_title="deploy", gitlab_project=None, gitlab_user=None, key_content=None, password=None, path="~/.ssh/id_ed25519", path_to_key=None, user=None):

        super(GitlabProjectDeployKeyAuthorized, self).__init__(var_names=["access_token", "bits", "can_push", "deploy_key_title", "gitlab_project", "gitlab_user", "key_content", "password", "path", "path_to_key", "user"])
        self._access_token = access_token
        self._bits = bits
        self._can_push = can_push
        self._deploy_key_title = deploy_key_title
        self._gitlab_project = gitlab_project
        self._gitlab_user = gitlab_user
        self._key_content = key_content
        self._password = password
        self._path = path
        self._path_to_key = path_to_key
        self._user = user

    @property
    def access_token(self):
        return self._access_token

    @access_token.setter
    def access_token(self, access_token):
        self._access_token = access_token

    @property
    def bits(self):
        return self._bits

    @bits.setter
    def bits(self, bits):
        self._bits = bits

    @property
    def can_push(self):
        return self._can_push

    @can_push.setter
    def can_push(self, can_push):
        self._can_push = can_push

    @property
    def deploy_key_title(self):
        return self._deploy_key_title

    @deploy_key_title.setter
    def deploy_key_title(self, deploy_key_title):
        self._deploy_key_title = deploy_key_title

    @property
    def gitlab_project(self):
        return self._gitlab_project

    @gitlab_project.setter
    def gitlab_project(self, gitlab_project):
        self._gitlab_project = gitlab_project

    @property
    def gitlab_user(self):
        return self._gitlab_user

    @gitlab_user.setter
    def gitlab_user(self, gitlab_user):
        self._gitlab_user = gitlab_user

    @property
    def key_content(self):
        return self._key_content

    @key_content.setter
    def key_content(self, key_content):
        self._key_content = key_content

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, password):
        self._password = password

    @property
    def path(self):
        return self._path

    @path.setter
    def path(self, path):
        self._path = path

    @property
    def path_to_key(self):
        return self._path_to_key

    @path_to_key.setter
    def path_to_key(self, path_to_key):
        self._path_to_key = path_to_key

    @property
    def user(self):
        return self._user

    @user.setter
    def user(self, user):
        self._user = user



frecklet_class = GitlabProjectDeployKeyAuthorized