GithubHelp home page GithubHelp logo

vmware-archive / cf-env Goto Github PK

View Code? Open in Web Editor NEW
7.0 26.0 8.0 148 KB

A small Java library for parsing the Cloud Foundry environment variables

License: BSD 2-Clause "Simplified" License

Java 100.00%

cf-env's Introduction

A small library for parsing the Cloud Foundry environment variables.

Cloud Foundry defines a number of environment variables, but at the moment, we only parse VCAP_SERVICES. We list the bound services by name, exposing the metadata and credentials for each, and provide convenient access to any SSL keys and certificates encoded in the credentials.

Using it

Download it from Bintray:

download

Or use it as a dependency in your build:

repositories {
    jcenter()
}

dependencies {
    compile group: 'io.pivotal.labs', name: 'cf-env', version: '0.0.1'
}

Incorporate it into your application:

import io.pivotal.labs.cfenv.CloudFoundryEnvironment;
import io.pivotal.labs.cfenv.CloudFoundryService;

@WebServlet("/ini")
public class ServicesAsIniFileServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CloudFoundryEnvironment environment;
        try {
            environment = new CloudFoundryEnvironment(System::getenv);
        } catch (CloudFoundryEnvironmentException e) {
            throw new ServletException(e);
        }

        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType("text/plain");

        try (PrintWriter out = response.getWriter()) {
            for (String serviceName : environment.getServiceNames()) {
                CloudFoundryService service = environment.getService(serviceName);
                out.println("[" + service.getName() + "]");
                out.println("label = " + service.getLabel());
                if (service.getPlan() != null) out.println("plan = " + service.getPlan());
                out.println("tags = " + service.getTags().stream().collect(Collectors.joining(", ")));
                Map<String, Object> credentials = service.getCredentials();
                credentials.forEach((name, value) -> out.println("credentials." + name + " = " + value));
                out.println();
            }
        }
    }
}

Most of the interesting methods are on the CloudFoundryService class, so have a look at that.

Developing it

Build it with Gradle:

./gradlew build

Release it with Gradle:

# you probably want to make these changes manually rather than like this
sed -i -e "s/^version = .*/version = 'x.y.z'/" build.gradle
echo -e "bintrayUser=pivotal-labs-london\nbintrayKey=..." >gradle.properties

./gradlew bintrayUpload

Create some SSL keys and certificates for testing the certificate handling:

# create a CA RSA key pair
openssl req -x509 -newkey 512 -subj /OU=IAmACertificateAuthority -nodes -keyout ca.key -out ca.crt

# create, sign, and convert to PKCS#8 a service RSA key pair
openssl req -newkey 512 -subj /OU=IAmAService -nodes -keyout service_rsa.key -out service_rsa.csr
openssl x509 -req -in service_rsa.csr -CAkey ca.key -CA ca.crt -CAcreateserial -out service_rsa.crt
openssl pkcs8 -topk8 -in service_rsa.key -nocrypt -out service_rsa.key8
openssl pkey -in service_rsa.key -pubout -out service_rsa.pub
chmod go-r service_rsa.key
ssh-keygen -e -f service_rsa.key -m pem >service_rsa.pub1

# create, sign, and convert to PKCS#8 a service EC key pair
openssl ecparam -name prime256v1 -genkey -noout -out service_ec.key
openssl req -new -key service_ec.key -subj /OU=IAmAService -nodes -out service_ec.csr
openssl x509 -req -in service_ec.csr -CAkey ca.key -CA ca.crt -CAcreateserial -out service_ec.crt
openssl pkcs8 -topk8 -in service_ec.key -nocrypt -out service_ec.key8
openssl pkey -in service_ec.key -pubout -out service_ec.pub

# create, sign, and convert to PKCS#8 a service DSA key pair
openssl dsaparam -genkey 512 -noout -out service_dsa.key
openssl req -new -key service_dsa.key -subj /OU=IAmAService -nodes -out service_dsa.csr
openssl x509 -req -in service_dsa.csr -CAkey ca.key -CA ca.crt -CAcreateserial -out service_dsa.crt
openssl pkcs8 -topk8 -in service_dsa.key -nocrypt -out service_dsa.key8
openssl pkey -in service_dsa.key -pubout -out service_dsa.pub

# create, sign, and convert to PKCS#8 a service Diffie-Hellman key pair
openssl dhparam 512 -out service_dh.params
openssl genpkey -paramfile service_dh.params -out service_dh.key8
openssl pkey -in service_dh.key8 -pubout -out service_dh.pub
openssl req -newkey 512 -subj /OU=IAmAService -nodes -keyout service_dh_dummy.key -out service_dh_dummy.csr
openssl x509 -req -in service_dh_dummy.csr -CAkey ca.key -CA ca.crt -CAcreateserial -force_pubkey service_dh.pub -out service_dh.crt

# diagnostic functions
dump_asn() { openssl asn1parse -i -in "$1"; }
dump_hex() { egrep -v '^--' <"$1" | base64 -D | hexdump | sed -E 's/^.{7}.?//' | tr '\n' ' ' | tr -s ' '; echo; }
dump_cert() { openssl x509 -in "$1" -text -noout; }
dump_rsa_key() { openssl rsa -in "$1" -text -noout; }
dump_ec_key() { openssl ec -in "$1" -text -noout; }

cf-env's People

Contributors

ipsi avatar kvmw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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  avatar

cf-env's Issues

environment without credentials causing null pointer exception

Following sample env. which has null value as credentials, causes NPE in CloudFoundryEnvironment class.

{
  "lionlogger": [
    {
      "credentials": null,
      "label": "foo-service",
      "name": "myapp-logger",
      "plan": "spider",
      "tags": [
        "Logging",
        "Cool Service",
        "Developer Tools"
      ]
    }
  ]
}

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.