One place for hosting & domains

      How To Mock Services Using Mountebank and Node.js


      The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program.

      Introduction

      In complex service-oriented architectures (SOA), programs often need to call multiple services to run through a given workflow. This is fine once everything is in place, but if the code you are working on requires a service that is still in development, you can be stuck waiting for other teams to finish their work before beginning yours. Additionally, for testing purposes you may need to interact with external vendor services, like a weather API or a record-keeping system. Vendors usually don’t give you as many environments as you need, and often don’t make it easy to control test data on their systems. In these situations, unfinished services and services outside of your control can make code testing frustrating.

      The solution to all of these problems is to create a service mock. A service mock is code that simulates the service that you would use in the final product, but is lighter weight, less complex, and easier to control than the actual service you would use in production. You can set a mock service to return a default response or specific test data, then run the software you’re interested in testing as if the dependent service were really there. Because of this, having a flexible way to mock services can make your workflow faster and more efficient.

      In an enterprise setting, making mock services is sometimes called service virtualization. Service virtualization is often associated with expensive enterprise tools, but you don’t need an expensive tool to mock a service. Mountebank is a free and open source service-mocking tool that you can use to mock HTTP services, including REST and SOAP services. You can also use it to mock SMTP or TCP requests.

      In this guide, you will build two flexible service-mocking applications using Node.js and Mountebank. Both of the mock services will listen to a specific port for REST requests in HTTP. In addition to this simple mocking behavior, the service will also retrieve mock data from a comma-separated values (CSV) file. After this tutorial, you’ll be able to mock all kinds of service behavior so you can more easily develop and test your applications.

      Prerequisites

      To follow this tutorial, you will need the following:

      Step 1 — Starting a Node.js Application

      In this step, you are going to create a basic Node.js application that will serve as the base of your Mountebank instance and the mock services you will create in later steps.

      Note: Mountebank can be used as a standalone application by installing it globally using the command npm install -g mountebank. You can then run it with the mb command and add mocks using REST requests.

      While this is the fastest way to get Mountebank up and running, building the Mountebank application yourself allows you to run a set of predefined mocks when the app starts up, which you can then store in source control and share with your team. This tutorial will build the Mountebank application manually to take advantage of this.

      First, create a new directory to put your application in. You can name it whatever you want, but in this tutorial we’ll name it app:

      Move into your newly created directory with the following command:

      To start a new Node.js application, run npm init and fill out the prompts:

      The data from these prompts will be used to fill out your package.json file, which describes what your application is, what packages it relies on, and what different scripts it uses. In Node.js applications, scripts define commands that build, run, and test your application. You can go with the defaults for the prompts or fill in your package name, version number, etc.

      After you finish this command, you'll have a basic Node.js application, including the package.json file.

      Now install the Mountebank npm package using the following:

      • npm install -save mountebank

      This command grabs the Mountebank package and installs it to your application. Make sure to use the -save flag in order to update your package.json file with Mountebank as a dependency.

      Next, add a start script to your package.json that runs the command node src/index.js. This script defines the entry point of your app as index.js, which you'll create in a later step.

      Open up package.json in a text editor. You can use whatever text editor you want, but this tutorial will use nano.

      Navigate to the "scripts" section and add the line "start": "node src/index.js". This will add a start command to run your application.

      Your package.json file should look similar to this, depending on how you filled in the initial prompts:

      app/package.json

      {
        "name": "diy-service-virtualization",
        "version": "1.0.0",
        "description": "An application to mock services.",
        "main": "index.js",
        "scripts": {
          "start": "node src/index.js"
        },
        "author": "Dustin Ewers",
        "license": "MIT",
        "dependencies": {
          "mountebank": "^2.0.0"
        }
      }
      

      You now have the base for your Mountebank application, which you built by creating your app, installing Mountebank, and adding a start script. Next, you'll add a settings file to store application-specific settings.

      Step 2 — Creating a Settings File

      In this step, you will create a settings file that determines which ports the Mountebank instance and the two mock services will listen to.

      Each time you run an instance of Mountebank or a mock service, you will need to specify what network port that service will run on (e.g., http://localhost:5000/). By putting these in a settings file, the other parts of your application will be able to import these settings whenever they need to know the port number for the services and the Mountebank instance. While you could directly code these into your application as constants, changing the settings later will be easier if you store them in a file. This way, you will only have to change the values in one place.

      Begin by making a directory called src from your app directory:

      Navigate to the folder you just created:

      Create a file called settings.js and open it in your text editor:

      Next, add settings for the ports for the main Mountebank instance and the two mock services you'll create later:

      app/src/settings.js

      module.exports = {
          port: 5000,
          hello_service_port: 5001,
          customer_service_port: 5002
      }
      

      This settings file has three entries: port: 5000 assigns port 5000 to the main Mountebank instance, hello_service_port: 5001 assigns port 5001 to the Hello World test service that you will create in a later step, and customer_service_port: 5002 assigns port 5002 to the mock service app that will respond with CSV data. If the ports here are occupied, feel free to change them to whatever you want. module.exports = makes it possible for your other files to import these settings.

      In this step, you used settings.js to define the ports that Mountebank and your mock services will listen to and made these settings available to other parts of your app. In the next step, you will build an initialization script with these settings to start Mountebank.

      Step 3 — Building the Initialization Script

      In this step, you're going to create a file that starts an instance of Mountebank. This file will be the entry point of the application, meaning that, when you run the app, this script will run first. You will add more lines to this file as you build new service mocks.

      From the src directory, create a file called index.js and open it in your text editor:

      To start an instance of Mountebank that will run on the port specified in the settings.js file you created in the last step, add the following code to the file:

      app/src/index.js

      const mb = require('mountebank');
      const settings = require('./settings');
      
      const mbServerInstance = mb.create({
              port: settings.port,
              pidfile: '../mb.pid',
              logfile: '../mb.log',
              protofile: '../protofile.json',
              ipWhitelist: ['*']
          });
      

      This code does three things. First, it imports the Mountebank npm package that you installed earlier (const mb = require('mountebank');). Then, it imports the settings module you created in the previous step (const settings = require('./settings');). Finally, it creates an instance of the Mountebank server with mb.create().

      The server will listen at the port specified in the settings file. The pidfile, logfile, and protofile parameters are for files that Mountebank uses internally to record its process ID, specify where it keeps its logs, and set a file to load custom protocol implementations. The ipWhitelist setting specifies what IP addresses are allowed to communicate with the Mountebank server. In this case, you're opening it up to any IP address.

      Save and exit from the file.

      After this file is in place, enter the following command to run your application:

      The command prompt will disappear, and you will see the following:

      • info: [mb:5000] mountebank v2.0.0 now taking orders - point your browser to http://localhost:5000/ for help

      This means your application is open and ready to take requests.

      Next, check your progress. Open up a new terminal window and use curl to send the following GET request to the Mountebank server:

      • curl http://localhost:5000/

      This will return the following JSON response:

      Output

      { "_links": { "imposters": { "href": "http://localhost:5000/imposters" }, "config": { "href": "http://localhost:5000/config" }, "logs": { "href": "http://localhost:5000/logs" } } }

      The JSON that Mountebank returns describes the three different endpoints you can use to add or remove objects in Mountebank. By using curl to send reqests to these endpoints, you can interact with your Mountebank instance.

      When you're done, switch back to your first terminal window and exit the application using CTRL + C. This exits your Node.js app so you can continue adding to it.

      Now you have an application that successfully runs an instance of Mountebank. In the next step, you will create a Mountebank client that uses REST requests to add mock services to your Mountebank application.

      Step 4 — Building a Mountebank Client

      Mountebank communicates using a REST API. You can manage the resources of your Mountebank instance by sending HTTP requests to the different endpoints mentioned in the last step. To add a mock service, you send a HTTP POST request to the imposters endpoint. An imposter is the name for a mock service in Mountebank. Imposters can be simple or complex, depending on the behaviors you want in your mock.

      In this step, you will build a Mountebank client to automatically send POST requests to the Mountebank service. You could send a POST request to the imposters endpoint using curl or Postman, but you'd have to send that same request every time you restart your test server. If you're running a sample API with several mocks, it will be more efficient to write a client script to do this for you.

      Begin by installing the node-fetch library:

      • npm install -save node-fetch

      The node-fetch library gives you an implementation of the JavaScript Fetch API, which you can use to write shorter HTTP requests. You could use the standard http library, but using node-fetch is a lighter weight solution.

      Now, create a client module to send requests to Mountebank. You only need to post imposters, so this module will have one method.

      Use nano to create a file called mountebank-helper.js:

      • nano mountebank-helper.js

      To set up the client, put the following code in the file:

      app/src/mountebank-helper.js

      const fetch = require('node-fetch');
      const settings = require('./settings');
      
      function postImposter(body) {
          const url = `http://127.0.0.1:${settings.port}/imposters`;
      
          return fetch(url, {
                          method:'POST',
                          headers: { 'Content-Type': 'application/json' },
                          body: JSON.stringify(body)
                      });
      }
      
      module.exports = { postImposter };
      

      This code starts off by pulling in the node-fetch library and your settings file. This module then exposes a function called postImposter that posts service mocks to Mountebank. Next, body: determines that the function takes JSON.stringify(body), a JavaScript object. This object is what you're going to POST to the Mountebank service. Since this method is running locally, you run your request against 127.0.0.1 (localhost). The fetch method takes the object sent in the parameters and sends the POST request to the url.

      In this step, you created a Mountebank client to post new mock services to the Mountebank server. In the next step, you'll use this client to create your first mock service.

      Step 5 — Creating Your First Mock Service

      In previous steps, you built an application that creates a Mountebank server and code to call that server. Now it's time to use that code to build an imposter, or a mock service.

      In Mountebank, each imposter contains stubs. Stubs are configuration sets that determine the response that an imposter will give. Stubs can be further divided into combinations of predicates and responses. A predicate is the rule that triggers the imposter's response. Predicates can use lots of different types of information, including URLs, request content (using XML or JSON), and HTTP methods.

      Looked at from the point of view of a Model-View-Controller (MVC) app, an imposter acts like a controller and the stubs like actions within that controller. Predicates are routing rules that point toward a specific controller action.

      To create your first mock service, create a file called hello-service.js. This file will contain the definition of your mock service.

      Open hello-service.js in your text editor:

      Then add the following code:

      app/src/hello-service.js

      const mbHelper = require('./mountebank-helper');
      const settings = require('./settings');
      
      function addService() {
          const response = { message: "hello world" }
      
          const stubs = [
              {
                  predicates: [ {
                      equals: {
                          method: "GET",
                          "path": "/"
                      }
                  }],
                  responses: [
                      {
                          is: {
                              statusCode: 200,
                              headers: {
                                  "Content-Type": "application/json"
                              },
                              body: JSON.stringify(response)
                          }
                      }
                  ]
              }
          ];
      
          const imposter = {
              port: settings.hello_service_port,
              protocol: 'http',
              stubs: stubs
          };
      
          return mbHelper.postImposter(imposter);
      }
      
      module.exports = { addService };
      
      

      This code defines an imposter with a single stub that contains a predicate and a response. Then it sends that object to the Mountebank server. This code will add a new mock service that listens for GET requests to the root url and returns { message: "hello world" } when it gets one.

      Let's take a look at the addService() function that the preceding code creates. First, it defines a response message hello world:

          const response = { message: "hello world" }
      ...
      

      Then, it defines a stub:

      ...
              const stubs = [
              {
                  predicates: [ {
                      equals: {
                          method: "GET",
                          "path": "/"
                      }
                  }],
                  responses: [
                      {
                          is: {
                              statusCode: 200,
                              headers: {
                                  "Content-Type": "application/json"
                              },
                              body: JSON.stringify(response)
                          }
                      }
                  ]
              }
          ];
      ...
      

      This stub has two parts. The predicate part is looking for a GET request to the root (/) URL. This means that stubs will return the response when someone sends a GET request to the root URL of the mock service. The second part of the stub is the responses array. In this case, there is one response, which returns a JSON result with an HTTP status code of 200.

      The final step defines an imposter that contains that stub:

      ...
          const imposter = {
              port: settings.hello_service_port,
              protocol: 'http',
              stubs: stubs
          };
      ...
      

      This is the object you're going to send to the /imposters endpoint to create an imposter that mocks a service with a single endpoint. The preceding code defines your imposter by setting the port to the port you determined in the settings file, setting the protocol to HTTP, and assigning stubs as the imposter's stubs.

      Now that you have a mock service, the code sends it to the Mountebank server:

      ...
          return mbHelper.postImposter(imposter);
      ...
      

      As mentioned before, Mountebank uses a REST API to manage its objects. The preceding code uses the postImposter() function that you defined earlier to send a POST request to the server to activate the service.

      Once you are finished with hello-service.js, save and exit from the file.

      Next, call the newly created addService() function in index.js. Open the file in your text editor:

      To make sure that the function is called when the Mountebank instance is created, add the following highlighted lines:

      app/src/index.js

      const mb = require('mountebank');
      const settings = require('./settings');
      const helloService = require('./hello-service');
      
      const mbServerInstance = mb.create({
              port: settings.port,
              pidfile: '../mb.pid',
              logfile: '../mb.log',
              protofile: '../protofile.json',
              ipWhitelist: ['*']
          });
      
      mbServerInstance.then(function() {
          helloService.addService();
      });
      

      When a Mountebank instance is created, it returns a promise. A promise is an object that does not determine its value until later. This can be used to simplify asynchronous function calls. In the preceding code, the .then(function(){...}) function executes when the Mountebank server is initialized, which happens when the promise resolves.

      Save and exit index.js.

      To test that the mock service is created when Mountebank initializes, start the application:

      The Node.js process will occupy the terminal, so open up a new terminal window and send a GET request to http://localhost:5001/:

      • curl http://localhost:5001

      You will receive the following response, signifying that the service is working:

      Output

      {"message": "hello world"}

      Now that you tested your application, switch back to the first terminal window and exit the Node.js application using CTRL + C.

      In this step, you created your first mock service. This is a test service mock that returns hello world in response to a GET request. This mock is meant for demonstration purposes; it doesn't really give you anything you couldn't get by building a small Express application. In the next step, you'll create a more complex mock that takes advantage of some of Mountebank's features.

      Step 6 — Building a Data-Backed Mock Service

      While the type of service you created in the previous step is fine for some scenarios, most tests require a more complex set of responses. In this step, you're going to create a service that takes a parameter from the URL and uses it to look up a record in a CSV file.

      First, move back to the main app directory:

      Create a folder called data:

      Open a file for your customer data called customers.csv:

      Add in the following test data so that your mock service has something to retrieve:

      app/data/customers.csv

      id,first_name,last_name,email,favorite_color 
      1,Erda,Birkin,[email protected],Aquamarine
      2,Cherey,Endacott,[email protected],Fuscia
      3,Shalom,Westoff,[email protected],Red
      4,Jo,Goulborne,[email protected],Red
      

      This is fake customer data generated by the API mocking tool Mockaroo, similar to the fake data you'd load into a customers table in the service itself.

      Save and exit the file.

      Then, create a new module called customer-service.js in the src directory:

      • nano src/customer-service.js

      To create an imposter that listens for GET requests on the /customers/ endpoint, add the following code:

      app/src/customer-service.js

      const mbHelper = require('./mountebank-helper');
      const settings = require('./settings');
      
      function addService() {
          const stubs = [
              {
                  predicates: [{
                      and: [
                          { equals: { method: "GET" } },
                          { startsWith: { "path": "/customers/" } }
                      ]
                  }],
                  responses: [
                      {
                          is: {
                              statusCode: 200,
                              headers: {
                                  "Content-Type": "application/json"
                              },
                              body: '{ "firstName": "${row}[first_name]", "lastName": "${row}[last_name]", "favColor": "${row}[favorite_color]" }'
                          },
                          _behaviors: {
                              lookup: [
                                  {
                                      "key": {
                                        "from": "path",
                                        "using": { "method": "regex", "selector": "/customers/(.*)$" },
                                        "index": 1
                                      },
                                      "fromDataSource": {
                                        "csv": {
                                          "path": "data/customers.csv",
                                          "keyColumn": "id"
                                        }
                                      },
                                      "into": "${row}"
                                    }
                              ]
                          }
                      }
                  ]
              }
          ];
      
          const imposter = {
              port: settings.customer_service_port,
              protocol: 'http',
              stubs: stubs
          };
      
          return mbHelper.postImposter(imposter);
      }
      
      module.exports = { addService };
      
      

      This code defines a service mock that looks for GET requests with a URL format of customers/<id>. When a request is received, it will query the URL for the id of the customer and then return the corresponding record from the CSV file.

      This code uses a few more Mountebank features than the hello service you created in the last step. First, it uses a feature of Mountebank called behaviors. Behaviors are a way to add functionality to a stub. In this case, you're using the lookup behavior to look up a record in a CSV file:

      ...
        _behaviors: {
            lookup: [
                {
                    "key": {
                      "from": "path",
                      "using": { "method": "regex", "selector": "/customers/(.*)$" },
                      "index": 1
                    },
                    "fromDataSource": {
                      "csv": {
                        "path": "data/customers.csv",
                        "keyColumn": "id"
                      }
                    },
                    "into": "${row}"
                  }
            ]
        }
      ...
      

      The key property uses a regular expression to parse the incoming path. In this case, you're taking the id that comes after customers/ in the URL.

      The fromDataSource property points to the file you're using to store your test data.

      The into property injects the result into a variable ${row}. That variable is referenced in the following body section:

      ...
        is: {
            statusCode: 200,
            headers: {
                "Content-Type": "application/json"
            },
            body: '{ "firstName": "${row}[first_name]", "lastName": "${row}[last_name]", "favColor": "${row}[favorite_color]" }'
        },
      ...
      

      The row variable is used to populate the body of the response. In this case, it's a JSON string with the customer data.

      Save and exit the file.

      Next, open index.js to add the new service mock to your initialization function:

      Add the highlighted line:

      app/src/index.js

      const mb = require('mountebank');
      const settings = require('./settings');
      const helloService = require('./hello-service');
      const customerService = require('./customer-service');
      
      const mbServerInstance = mb.create({
              port: settings.port,
              pidfile: '../mb.pid',
              logfile: '../mb.log',
              protofile: '../protofile.json',
              ipWhitelist: ['*']
          });
      
      mbServerInstance.then(function() {
          helloService.addService();
          customerService.addService();
      });
      
      

      Save and exit the file.

      Now start Mountebank with npm start. This will hide the prompt, so open up another terminal window. Test your service by sending a GET request to localhost:5002/customers/3. This will look up the customer information under id 3.

      • curl localhost:5002/customers/3

      You will see the following response:

      Output

      { "firstName": "Shalom", "lastName": "Westoff", "favColor": "Red" }

      In this step, you created a mock service that read data from a CSV file and returned it as a JSON response. From here, you can continue to build more complex mocks that match the services you need to test.

      Conclusion

      In this article you created your own service-mocking application using Mountebank and Node.js. Now you can build mock services and share them with your team. Whether it's a complex scenario involving a vendor service you need to test around or a simple mock while you wait for another team to finish their work, you can keep your team moving by creating mock services.

      If you want to learn more about Mountebank, check out their documentation. If you'd like to containerize this application, check out Containerizing a Node.js Application for Development With Docker Compose. If you'd like to run this application in a production-like environment, check out How To Set Up a Node.js Application for Production on Ubuntu 18.04.



      Source link

      How To Install Go on Debian 9


      Introduction

      Go, also known as golang, is a modern, open-source programming language developed by Google. Increasingly popular for many applications, Go takes a minimalist approach to development, helping you build reliable and efficient software.

      This tutorial will guide you through downloading and installing Go, as well as compiling and executing a basic “Hello, World!” program on a Debian 9 server.

      Prerequisites

      To complete this tutorial, you will need access to a Debian 9 server and a non-root user with sudo privileges, as described in Initial Server Setup with Debian 9.

      Step 1 — Downloading Go

      In this step, we’ll install Go on your server.

      First, install curl so you will be able to grab the latest Go release:

      Next, visit the official Go downloads page and find the URL for the current binary release's tarball. Make sure you copy the link for the latest version that is compatible with a 64-bit architecture.

      From your home directory, use curl to retrieve the tarball:

      • curl -O https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz

      Although the tarball came from a genuine source, it is best practice to verify both the authenticity and integrity of items downloaded from the Internet. This verification method certifies that the file was neither tampered with nor corrupted or damaged during the download process. The sha256sum command produces a unique 256-bit hash:

      • sha256sum go1.12.5.linux-amd64.tar.gz

      Output

      go1.12.5.linux-amd64.tar.gz aea86e3c73495f205929cfebba0d63f1382c8ac59be081b6351681415f4063cf go1.12.5.linux-amd64.tar.gz

      Compare the hash in your output to the checksum value on the Go download page. If they match, then it is safe to conclude that the download is legitimate.

      With Go downloaded and the integrity of the file validated, let's proceed with the installation.

      Step 2 — Installing Go

      We’ll now use tar to extract the tarball. The x flag tells tar to extract, v tells it we want verbose output, including a list of the files being extracted, and f tells it we'll specify a filename:

      • tar xvf go1.12.5.linux-amd64.tar.gz

      You should now have a directory called go in your home directory. Recursively change the owner and group of this directory to root, and move it to /usr/local:

      • sudo chown -R root:root ./go
      • sudo mv go /usr/local

      Note: Although /usr/local/go is the officially-recommended location, some users may prefer or require different paths.

      At this point, using Go would require specifying the full path to its install location in the command line. To make interacting with Go more user-friendly, we will set a few paths.

      Step 2 — Setting Go Paths

      In this step, we'll set some paths in your environment.

      First, set Go's root value, which tells Go where to look for its files:

      At the end of the file, add the following lines:

      export GOPATH=$HOME/work
      export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
      

      If you chose a different installation location for Go, then you should add the following lines to this file instead of the lines shown above. In this example, we are adding the lines that would be required if you installed Go in your home directory:

      export GOROOT=$HOME/go
      export GOPATH=$HOME/work
      export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
      

      With the appropriate lines pasted into your profile, save and close the file.

      Next, refresh your profile by running:

      With the Go installation in place and the necessary environment paths set, let’s confirm that our setup works by composing a short program.

      Step 3 — Testing Your Installation

      Now that Go is installed and the paths are set for your server, you can ensure that Go is working as expected.

      Create a new directory for your Go workspace, which is where Go will build its files:

      Then, create a directory hierarchy in this folder so that you will be able to create your test file. We’ll use the directory my_project as an example:

      • mkdir -p work/src/my_project/hello

      Next, you can create a traditional "Hello World" Go file:

      • nano ~/work/src/my_project/hello/hello.go

      Inside your editor, add the following code to the file, which uses the main Go packages, imports the formatted IO content component, and sets a new function to print "Hello, World!" when run:

      ~/work/src/my_project/hello/hello.go

      package main
      
      import "fmt"
      
      func main() {
          fmt.Printf("Hello, World!n")
      }
      

      When it runs, this program will print "Hello, World!," indicating that Go programs are compiling correctly.

      Save and close the file, then compile it by invoking the Go command install:

      • go install my_project/hello

      With the program compiled, you can run it by executing the command:

      Go is successfully installed and functional if you see the following output:

      Output

      Hello, World!

      You can see where the compiled hello binary is installed by using the which command:

      Output

      /home/sammy/work/bin/hello

      The “Hello, World!” program established that you have a Go development environment.

      Conclusion

      By downloading and installing the latest Go package and setting its paths, you now have a system to use for Go development. To learn more about working with Go, see our development series How To Code in Go. You can also consult the official documentation on How to Write Go Code.

      Additionally, you can read some Go tips from our development team at DigitalOcean.



      Source link

      How To Use Certbot Standalone Mode to Retrieve Let’s Encrypt SSL Certificates on CentOS 7


      Introduction

      Let’s Encrypt is a service offering free SSL certificates through an automated API. The most popular Let’s Encrypt client is EFF’s Certbot.

      Certbot offers a variety of ways to validate your domain, fetch certificates, and automatically configure Apache and Nginx. In this tutorial, we’ll discuss Certbot’s standalone mode and how to use it to secure other types of services, such as a mail server or a message broker like RabbitMQ.

      We won’t discuss the details of SSL configuration, but when you are done you will have a valid certificate that is automatically renewed. Additionally, you will be able to automate reloading your service to pick up the renewed certificate.

      Prerequisites

      Before starting this tutorial, you will need:

      • An CentOS 7 server with a non-root, sudo-enabled user, as detailed in this CentOS 7 initial server setup tutorial.
      • A domain name pointed at your server, which you can accomplish by following “How to Set Up a Host Name with DigitalOcean.” This tutorial will use example.com throughout.
      • Port 80 or 443 must be unused on your server. If the service you’re trying to secure is on a machine with a web server that occupies both of those ports, you’ll need to use a different mode such as Certbot’s webroot mode.

      Step 1 — Installing Certbot

      Certbot is packaged in an extra repository called Extra Packages for Enterprise Linux (EPEL). To enable this repository on CentOS 7, run the following yum command:

      • sudo yum --enablerepo=extras install epel-release

      Afterwards, the certbot package can be installed with yum:

      You may confirm your install was successful by calling the certbot command:

      Output

      certbot 0.31.0

      Now that we have Certbot installed, let's run it to get our certificate.

      Step 2 — Running Certbot

      Certbot needs to answer a cryptographic challenge issued by the Let's Encrypt API in order to prove we control our domain. It uses ports 80 (HTTP) or 443 (HTTPS) to accomplish this. If you're using a firewall, open up the appropriate port now. For firewalld this would be something like the following:

      • sudo firewall-cmd --add-service=http
      • sudo firewall-cmd --runtime-to-permanent

      Substitute https for http above if you're using port 443.

      We can now run Certbot to get our certificate. We'll use the --standalone option to tell Certbot to handle the challenge using its own built-in web server. The --preferred-challenges option instructs Certbot to use port 80 or port 443. If you're using port 80, you want --preferred-challenges http. For port 443 it would be --preferred-challenges tls-sni. Finally, the -d flag is used to specify the domain you're requesting a certificate for. You can add multiple -d options to cover multiple domains in one certificate.

      • sudo certbot certonly --standalone --preferred-challenges http -d example.com

      When running the command, you will be prompted to enter an email address and agree to the terms of service. After doing so, you should see a message telling you the process was successful and where your certificates are stored:

      Output

      IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/example.com/privkey.pem Your cert will expire on 2018-10-09. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le

      We've got our certificates. Let's take a look at what we downloaded and how to use the files with our software.

      Step 3 — Configuring Your Application

      Configuring your application for SSL is beyond the scope of this article, as each application has different requirements and configuration options, but let's take a look at what Certbot has downloaded for us. Use ls to list out the directory that holds our keys and certificates:

      • sudo ls /etc/letsencrypt/live/example.com

      Output

      cert.pem chain.pem fullchain.pem privkey.pem README

      The README file in this directory has more information about each of these files. Most often you'll only need two of these files:

      • privkey.pem: This is the private key for the certificate. This needs to be kept safe and secret, which is why most of the /etc/letsencrypt directory has very restrictive permissions and is accessible by only the root user. Most software configuration will refer to this as something similar to ssl-certificate-key or ssl-certificate-key-file.
      • fullchain.pem: This is our certificate, bundled with all intermediate certificates. Most software will use this file for the actual certificate, and will refer to it in their configuration with a name like 'ssl-certificate'.

      For more information on the other files present, refer to the "Where are my certificates" section of the Certbot docs.

      Some software will need its certificates in other formats, in other locations, or with other user permissions. It is best to leave everything in the letsencrypt directory, and not change any permissions in there (permissions will just be overwritten upon renewal anyway), but sometimes that's just not an option. In that case, you'll need to write a script to move files and change permissions as needed. This script will need to be run whenever Certbot renews the certificates, which we'll talk about next.

      Step 4 — Enabling Automatic Certificate Renewal

      Let's Encrypt's certificates are only valid for ninety days. This is to encourage users to automate their certificate renewal process. The certbot package we installed includes a systemd timer to check for renewals twice a day, but it is disabled by default. Enable the timer by running the following command:

      • sudo systemctl enable --now certbot-renew.timer

      Output

      Created symlink from /etc/systemd/system/timers.target.wants/certbot-renew.timer to /usr/lib/systemd/system/certbot-renew.timer.

      You may verify the status of the timer using systemctl:

      • sudo systemctl status certbot-renew.timer

      Output

      ● certbot-renew.timer - This is the timer to set the schedule for automated renewals Loaded: loaded (/usr/lib/systemd/system/certbot-renew.timer; enabled; vendor preset: disabled) Active: active (waiting) since Fri 2019-05-31 15:10:10 UTC; 48s ago

      The timer should be active. Certbot will now automatically renew any certificates on this server whenever necessary.

      Step 5 — Running Tasks When Certificates are Renewed

      Now that our certificates are renewing automatically, we need a way to run certain tasks after a renewal. We need to at least restart or reload our server to pick up the new certificates, and as mentioned in Step 3 we may need to manipulate the certificate files in some way to make them work with the software we're using. This is the purpose of Certbot's renew_hook option.

      To add a renew_hook, we update Certbot's renewal config file. Certbot remembers all the details of how you first fetched the certificate, and will run with the same options upon renewal. We just need to add in our hook. Open the config file with you favorite editor:

      • sudo vi /etc/letsencrypt/renewal/example.com.conf

      A text file will open with some configuration options. Add your hook on the last line:

      /etc/letsencrypt/renewal/example.com.conf

      renew_hook = systemctl reload rabbitmq
      

      Update the command above to whatever you need to run to reload your server or run your custom file munging script. Usually, on CentOS, you’ll mostly be using systemctl to reload a service. Save and close the file, then run a Certbot dry run to make sure the syntax is ok:

      • sudo certbot renew --dry-run

      If you see no errors, you're all set. Certbot is set to renew when necessary and run any commands needed to get your service using the new files.

      Conclusion

      In this tutorial, we've installed the Certbot Let's Encrypt client, downloaded an SSL certificate using standalone mode, and enabled automatic renewals with renew hooks. This should give you a good start on using Let's Encrypt certificates with services other than your typical web server.

      For more information, please refer to Certbot's documentation.



      Source link