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

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_rsa

The path to the private ssh key.

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:
    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 | true_if_empty ::}}'
    path: '{{:: path ::}}'
    password: '{{:: password ::}}'
    user: '{{:: user ::}}'
- 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 ::}}.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:
      - hcloud
      ansible-tasklist:
      - internally-register-public-ssh-key.at.yml
  task:
    become: false
  vars:
    __path_to_key__: '{{:: path ::}}'
    __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 ::}}'
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]
  --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 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.
         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 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
    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
    user: str = None


    def __post_init__(self):
        super(GitlabProjectDeployKeyAuthorized, self).__init__(var_names=["access_token", "can_push", "deploy_key_title", "gitlab_project", "gitlab_user", "key_content", "password", "path", "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.
         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 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, can_push=None, deploy_key_title="deploy", gitlab_project=None, gitlab_user=None, key_content=None, password=None, path="~/.ssh/id_rsa", user=None):

        super(GitlabProjectDeployKeyAuthorized, self).__init__(var_names=["access_token", "can_push", "deploy_key_title", "gitlab_project", "gitlab_user", "key_content", "password", "path", "user"])
        self._access_token = access_token
        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._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 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 user(self):
        return self._user

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



frecklet_class = GitlabProjectDeployKeyAuthorized