Code Monkey home page Code Monkey logo

ansodium's Introduction

ansodium

Ansible Galaxy collection repository.
Simple sodium wrapper module for ansible, based on PyNaCl

My use case

In order to automate some Github related things, I have to use the Actions/Secrets endpoint. To create or update a secrets I needed to encrypt data using the Sodium library and I found nothing Ansible related to do that. Firstly I created a NodeJS script with tweetsodium, but it was not really usable in Ansible context. So, ... I writed down a module. For now it is deadly simple, it just generate Keypair, encrypt and decrypt, but it works.

If you want an example of what it looks like, then this is the roles which embbed it:

- name: Github - fetch project public key
  uri:
    url: "{{ github_api_repo_url }}/actions/secrets/public-key"
    headers:
      Authorization: "token {{ github_personnal_token }}"
    status_code: 200
  register: github_pubkey

- name: Github - Encrypt token
  ansodium:
    pubkey: "{{ github_pubkey.json.key }}"
    data: "{{ secret_token }}"
  register: encrypt_output

- name: Github - Push new secrets
  uri:
    url: "{{ github_api_repo_url }}/actions/secrets/SECRET_TOKEN"
    body:
      encrypted_value: "{{ encrypt_output.encrypted }}"
      key_id: "{{ github_pubkey.json.key_id }}"
    body_format: json
    method: PUT
    status_code: 201,204
    headers:
      Authorization: "token {{ github_personnal_token }}"

Install


Install it via ansible-galaxy (recommended):

ansible-galaxy collection install marmorag.ansodium
NOTE: Installing collections with ansible-galaxy is only supported in ansible 2.9+

You will need the PyNacl Python module to be installed.

pip install pynacl

Or use the provided install roles

roles:
    - { role: marmorag.ansodium.install }

Install it manually:

Refering to ansible docs to install a module, either :

  • add directory to ANSIBLE_LIBRARY environment variable
  • put it in ~/.ansible/plugins/modules/
  • put in in /usr/share/ansible/plugins/modules/
git clone https://github.com/marmorag/ansodium 
cd ./ansodium

mkdir -p ~/.ansible/plugins/modules
cp ./ansodium.py ~/.ansible/plugins/modules

Or, to use it in one playbook/role only:

  • put it in a library directory in the directory containing your playbook
  • put it in a library directory in the directory containing your role

In any case, you can check that module is correctly installed with

ansible-doc -t module ansodium

Of course PyNacl python package is required in that case too.

Usage


Generate keypair

- name: generate keypair
  ansodium:
    keypair: true

Here keypair specify to generate a random keypair

Output format :

{
    "private_key": "<b64 encoded private key>",
    "public_key": "<b64 encoded public key>",
    "changed": true,
    "failed": false
}

Encrypt

- name: encrypt data
  ansodium:
    pubkey: "<public key to encrypt with>"
    data: "<data you want to encrypt>"

Where pubkey and data is required.

Output format :

{
    "encrypted": "<encrypted data>",
    "original_data": "<original data>",
    "changed": true,
    "failed": false
}

Decrypt:

- name: decrypt data
  ansodium:
    encrypt: false 
    prikey: "<private key to decrypt with>"
    data: "<data you want to decrypt>"

Where prikey, data and encrypt is required in order to decrypt data.

Output format :

{
    "decrypted": "<decrypted data>",
    "original_data": "<original encrypted data>",
    "changed": true,
    "failed": false
}

ansodium's People

Contributors

marmorag avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

bindugouthu

ansodium's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.