Git Product home page Git Product logo

oc-login's Introduction

oc-login

CI checks oc login workflow Multiplatform Workflow Link checker

tag badge license badge size badge

oc-login is a GitHub Action to log into an OpenShift cluster, and preserve that Kubernetes context for the remainder of the job.

See the OpenShift Documentation for an explanation of a log in using oc, which this action wraps.

Getting Started with the Action (or, see example)

  1. oc must be installed on the GitHub Action runner you specify.

  2. Find your OpenShift Server URL.

    • If you have already performed an oc login locally, run oc whoami --show-server.
    • Otherwise, in the Web Console, click your profile name, then Copy Login Commmand. The --server option contains the OpenShift server URL.
    • At this time, the cluster must be available on the internet, so the Action runner can access it.
  3. Decide how you are going to log into the OpenShift server from the action.

    • The recommended approach is to create a functional Service Account and use its token.
      • The primary advantage of Service Accounts is that by default, their tokens do not expire.
    • You can also use a personal token.
      • If you have already logged in locally, use oc whoami --show-token.
      • Otherwise, you can retrieve your token from the Web Console, using the same steps as in Step 2.
      • Personal tokens generally expire after 12 or 24 hours, depending on how your cluster is configured.
    • You can also use your personal credentials (username and password).
    • If both token and credentials are provided, the credentials take precedence and the token is ignored.
  4. Determine how you are going to manage SSL/TLS certificate verification. If your cluster uses self-signed certificates (which is the default), the GitHub runner will not recognize the certificate and will not allow you to issue HTTPS requests.

    • The easiest way to get around this is to set the insecure_skip_tls_verify input to true.
    • You can also obtain the self-signed certificate data (from a .crt file) and use the certificate_authority_data input.
  1. Store the Server URL and any credentials (passwords, tokens, or certificates) in GitHub Secrets.
  1. Create your workflow.

Example Workflow Step

steps:
  - name: Authenticate and set context
    uses: redhat-actions/oc-login@v1
    env:
      # These can be stored in secrets, if desired.
      OPENSHIFT_USER: my-username
      OPENSHIFT_NAMESPACE: my-namespace

    with:
      # URL to your OpenShift cluster.
      # Refer to Step 2.
      openshift_server_url: ${{ secrets.OPENSHIFT_SERVER }}

      # Authentication Token. Can use username and password instead.
      # Refer to Step 3.
      openshift_token: ${{ secrets.OPENSHIFT_TOKEN }}

      # Credentials, if desired instead of token.
      # Username and password override token if they are set.
      openshift_username: ${{ env.OPENSHIFT_USER }}
      openshift_password: ${{ secrets.OPENSHIFT_PASSWORD }}

      # Disables SSL cert checking. Use this if you don't have the certificate authority data.
      insecure_skip_tls_verify: true
      # This method is more secure, if the certificate from Step 4 is available.
      certificate_authority_data: ${{ secrets.CA_DATA }}

      # Optional - this sets your Kubernetes context's current namespace after logging in.
      namespace: ${{ env.OPENSHIFT_NAMESPACE }}

Other Optional Inputs

By default, the cluster name is masked, since it can be used to derive the server URL. Set reveal_cluster_name to true to skip masking the cluster name if you wish it to be visible in the workflow logs.

Result

If the oc-login step succeeds:

  • The Kubernetes config generated by oc will be saved into a file
  • The KUBECONFIG environment variable will be set to that file
  • The rest of the job will have access to the Kubernetes context, so you can run any oc or kubectl commands you like.

Note that if you start another job, the new job will use a new container, and you will have to run this action again. Since the kubeconfig contains secrets, it should not be uploaded in an artifact, and cannot set as an output from this action.

Runner OS Support

See the multiplatform workflow. All 3 runner operating systems are supported.

See the note in Step 1 regarding installing oc into the non-Ubuntu runners.

Contributing

Anyone can contribute to this project.

This project uses a pre-commit hook to ensure consistency between the source code and distribution, as well as between the source code and action.yml. This is verified by the CI Checks workflow.

After cloning this repository, install the hooks by running:

cd oc-login/
cp -r scripts/git-hooks/ .git/hooks/

Then run npm ci to install dependencies, and you should be ready to develop.

Changelog

See CHANGELOG.md.

oc-login's People

Contributors

dependabot[bot] avatar divyansh42 avatar sepulvedablanco avatar smlgbl avatar tetchel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

oc-login's Issues

Add 'post' step that logs out

if the workflow is using a self-hosted runner, a user would likely want to log out after their oc workflow finishes.

[BUG] redhat-actions/oc-login using deprecated version of Node.js

Version

redhat-actions/[email protected]

Describe the bug

Node.js 12 actions are deprecated. For more information see: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/. Please update the following actions to use Node.js 16: redhat-actions/openshift-tools-installer, redhat-actions/oc-login

Steps to reproduce, workflow links, screenshots

Any attempt to use the actions should show this warning.
I already reported in 2 other actions that apparently also used deprecated versions of Node.js. here & here

OC Client v3 does not understand --current

Version

redhat-actions/oc-login@v1

with: oc_version: '3.11.374' or any 3.x actually

Describe the bug

It seems that by default, the oc-login action will use the --current flag when setting the context.

Unfortunately, --current does not exist in oc config set-context v3

The action should not try to set --current when used in conjuntion with oc v3.

Steps to reproduce, workflow links, screenshots

Use the action with:

      - name: Install oc
        uses: redhat-actions/oc-installer@v1
        with:
          oc_version: '3.11.374'


      - name: Authenticate and set context
        uses: Actions/oc-login@v1
        with:
          # URL to your OpenShift cluster.
          # Refer to Step 2.
          openshift_server_url:  ${{ env.OPENSHIFT_URL }}
          # Authentication Token. Can use username and password instead.
          # Refer to Step 3.
          openshift_token: ${{ env.OC_TOKEN }}
          # Optional - this sets your Kubernetes context's current namespace after logging in.
          namespace: ${{ env.OPENSHIFT_NAMESPACE }}

and you'll get:

*** Suppressing command output
Set current context's namespace to "myns"
/usr/local/bin/oc config set-context --current --namespace=myns
Error: unknown flag: --current


Usage:
  oc config set-context NAME [--cluster=cluster_nickname] [--user=user_nickname] [--namespace=namespace] [flags]

Examples:
  # Set the user field on the gce context entry without touching other values
  oc config set-context gce --user=cluster-admin

Use "oc options" for a list of global command-line options (applies to all commands).

Error: Error: oc exited with code 1
Error: unknown flag: --current

Workaround:

don't set the optional namespace ; context won't be set to a given namespace, but at least login will succeed

[QUESTION] How to use oc-login with username and password?

Question

I followed the guide and added my openshift username and password as secrets in github. Now, when i run the oc-login action it throws an error claiming

Login failed (401 Unauthorized)
Verify you have provided the correct credentials.

Strangely, using the oc login -p "mypassword" -u "myusername" also throws the same error. I can however log in successfully via the web UI.

I am using Openshift Sandbox, hence I cannot switch to a service account.

Any ideas?

Edit: logging in via the command line works by omitting the password, but how do you omit the password for the action? it is not possible to my knowledge

oc login difference

I just noticed that:

oc login --token=<token> --server=<server>

sets a namespace in kubeconfig.

but:

 - name: Authenticate and set context
        uses: redhat-actions/oc-login@v1

        with:
          openshift_server_url: ${{ secrets.OPENSHIFT_SERVER }}
          openshift_token: ${{ secrets.OPENSHIFT_TOKEN }}

does not.

Resulting in different behavior - and in case of openshift sandbox it means oc login works and I can deploy, but oc login with github action I end up in the wrong place.

Workaround is to set namespace explicitly but I would expect the two would/should behave the same ?

[QUESTION] CA data for certificate_authority_data option

Question

Hello, I am trying to setup an action that will oc-login with TLS enabled. I am passing in certificate_authority_data from a repository secret, which contains the certificate data from the tls.crt field from openshift-service-ca/signing-key secret from the cluster.

Running oc-login with the parameters I pass to the action works fine, however I'm always met with error: The server uses a certificate signed by unknown authority. You may need to use the --certificate-authority flag to provide the path to a certificate file for the certificate authority, or --insecure-skip-tls-verify to bypass the certificate check and use insecure connections. when running from the action.

Would I need to populate the certificate data with something else?

Context

Step snippet

  - name: Authenticate and set context
    uses: redhat-actions/oc-login@v1
    with:
      openshift_server_url: ${{ secrets.OPENSHIFT_SERVER }}
      openshift_token: ${{ secrets.OPENSHIFT_TOKEN }}
      certificate_authority_data: ${{ secrets.CA_DATA }}
      namespace: ci-test

The secret CA_DATA contains the certificate in it's standard format like so:

-----BEGIN CERTIFICATE-----
            ***
-----END CERTIFICATE-----

[FEATURE] Make the openshift_server_url input optional

Is your feature request related to a problem? Please describe.

As pointed out in the OpenShift documentation on how to use a service account, see step #2 of the section titled "Using a service account’s credentials externally". The API token generated for service account allows login using only the token, omitting the server url.

Describe the solution you'd like

It would be nice if the input openshift_server_url of this action was optional.

Describe alternatives you've considered

Alternative is to put the server url, which is more likely to change than the token itself in our use case.

Additional context

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.