Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
Skip mounting services on incompatible versions
Browse files Browse the repository at this point in the history
This commit adds guard logic to the Clojure FactsUploadService that ensures
routes and status info are not mounted if the plug-in is loaded by an
incompatible Puppet Server version. Compatibility is currently defined by
the X.Y components of the version number and restricted to Puppet Server
5.1 and 5.2.

A test against Puppet Server 5.3 is added to ensure the noop behavior is
triggered.

Ref. issue #2.
  • Loading branch information
Sharpie committed Apr 12, 2018
1 parent 192fd61 commit 35750fb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 24 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ matrix:
- jdk: openjdk8
script: 'bundle exec rake test:integration'
env: PUPPETSERVER_VERSION=5.1.5
- jdk: oraclejdk8
script: 'bundle exec rake test:integration'
env: PUPPETSERVER_VERSION=5.1.5
- jdk: openjdk8
script: 'bundle exec rake test:integration'
env: PUPPETSERVER_VERSION=5.2.0
- jdk: oraclejdk8
- jdk: openjdk8
script: 'bundle exec rake test:integration'
env: PUPPETSERVER_VERSION=5.1.5
env: PUPPETSERVER_VERSION=5.3.0

- jdk: openjdk8
sudo: required
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Fixed

- The `facts_upload.jar` plugin will no longer activate when loaded by
a Puppet Server version that the plugin is not compatible with.

## [1.0.2] - 2018-02-21
### Added
Expand Down
62 changes: 48 additions & 14 deletions src/clj/puppetlabs/services/facts_upload/facts_upload_service.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns puppetlabs.services.facts-upload.facts-upload-service
(:require
[clojure.edn :as edn]
[clojure.string :as string]
[clojure.tools.logging :as log]
[puppetlabs.services.facts-upload.facts-upload-core :as core]
[puppetlabs.trapperkeeper.core :refer [defservice]]
Expand All @@ -8,11 +10,37 @@


(def version
"The service version, from project.clj"
"The facts_upload module version, from project.clj"
(status-core/get-artifact-version "sharpie" "facts-upload"))

(def puppetserver-version
"Retrieve Puppet Server version in use.
This function retrieves just the X and Y components of the version
and casts them to a floating point number that can be compared."
(try
(as-> (status-core/get-artifact-version "puppetlabs" "puppetserver") x
(string/split x #"\.")
(take 2 x)
(string/join "." x)
(edn/read-string x))
(catch Exception e
(log/errorf "Couldn't determine Puppet Server version %s" e)
nil)))

(defprotocol FactsUploadService)


(defn compatible-puppetserver-version?
"Determine whether Puppet Server is compatible with facts_upload.
This function returns a simple boolean value."
[]
(if (nil? puppetserver-version)
false
(and (>= puppetserver-version 5.1)
(< puppetserver-version 5.3))))

(defservice facts-upload-service
FactsUploadService
[[:StatusService register-status]
Expand All @@ -26,27 +54,33 @@
(init [this context]
;; TODO: Only build the handler if we detect a compatible version of
;; Puppet Server.
(log/info "Initializing FileServing service")
(let [puppet-config (get-config)
auth-handler (get-auth-handler)
jruby-service (services/get-service this :JRubyPuppetService)
jruby-handler (core/create-wrapped-jruby-handler puppet-config
current-code-id
jruby-service
auth-handler)
facts-handler (core/create-request-handler jruby-handler)]
(if (compatible-puppetserver-version?)
(do
(log/info "Initializing FactsUpload service")
(let [puppet-config (get-config)
auth-handler (get-auth-handler)
jruby-service (services/get-service this :JRubyPuppetService)
jruby-handler (core/create-wrapped-jruby-handler puppet-config
current-code-id
jruby-service
auth-handler)
facts-handler (core/create-request-handler jruby-handler)]

(register-status "facts-upload-service" version 1
(core/create-status-callback context))
(register-status "facts-upload-service" version 1
(core/create-status-callback context))

(assoc context :request-handler facts-handler)))
(assoc context :request-handler facts-handler)))
(do
(log/errorf "The facts_upload module is not compatible with Puppet Server %s and should be removed from this node."
puppetserver-version)
context)))


(start [this context]
;; FIXME: Use the WebroutingService to lookup which path the MasterService
;; is mounted at.
(when-let [handler (:request-handler context)]
(log/info "Starting FactsUploadService")
(log/info "Starting FactsUpload service")
(add-ring-handler handler "/puppet/v3/facts"))

context))
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@

;; Test Configuration

(def puppetserver-version
"Version of Puppet Server that we are currently running against."
(status-core/get-artifact-version "puppetlabs" "puppetserver"))

(defn test-resource
"Locates a path within the registered Java resource directories and returns
a fully qualified path"
Expand Down Expand Up @@ -98,7 +94,7 @@


(deftest ^:integration facts-upload-service
(printf "Testing against Puppet Server version: %s%n" puppetserver-version)
(printf "Testing against Puppet Server version: %s%n" facts-upload/puppetserver-version)

(tst-bootstrap/with-app-with-config app app-services base-config
(let [jruby-service (tk-app/get-service app :JRubyPuppetService)
Expand All @@ -124,8 +120,15 @@
(finally
(jruby-testutils/return-instance jruby-service jruby-instance :facts-upload-endpoint-test)))

(testing "facts-upload plugin version number is available via status service"
(testing "facts-upload plugin is enabled appropriately"
(let [response (GET "/status/v1/services?level=debug")
body (-> response :body json/parse-string)]
body (-> response :body json/parse-string)
;; When run against an incompatible Puppet Server version, we
;; expect the TK status service to return a response that does
;; not reference the facts-upload service, i.e. nil, which
;; indicates the service was not mounted.
expected-version (if (facts-upload/compatible-puppetserver-version?)
facts-upload/version
nil)]
(is (= 200 (:status response)))
(is (= facts-upload/version (get-in body ["facts-upload-service" "service_version"]))))))))
(is (= expected-version (get-in body ["facts-upload-service" "service_version"]))))))))

0 comments on commit 35750fb

Please sign in to comment.