Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /dbg certs command #3799

Merged
merged 1 commit into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions cmd/dbg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
const (
backendsPath = "/configuration/backends"
generalPath = "/configuration/general"
certsPath = "/configuration/certs"
)

func main() {
Expand Down Expand Up @@ -70,6 +71,24 @@ func main() {
}
backendsCmd.AddCommand(backendsGetCmd)

certCmd := &cobra.Command{
Use: "certs",
Short: "Inspect dynamic SSL certificates",
}

certGetCmd := &cobra.Command{
Use: "get [hostname]",
Short: "Get the dynamically-loaded certificate information for the given hostname",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
certGet(args[0])
return nil
},
}
certCmd.AddCommand(certGetCmd)

rootCmd.AddCommand(certCmd)

generalCmd := &cobra.Command{
Use: "general",
Short: "Output the general dynamic lua state",
Expand Down Expand Up @@ -102,7 +121,7 @@ func backendsAll() {
return
}
if statusCode != 200 {
fmt.Printf("Nginx returned code %v", statusCode)
fmt.Printf("Nginx returned code %v\n", statusCode)
return
}

Expand All @@ -123,7 +142,7 @@ func backendsList() {
return
}
if statusCode != 200 {
fmt.Printf("Nginx returned code %v", statusCode)
fmt.Printf("Nginx returned code %v\n", statusCode)
return
}

Expand All @@ -148,7 +167,7 @@ func backendsGet(name string) {
return
}
if statusCode != 200 {
fmt.Printf("Nginx returned code %v", statusCode)
fmt.Printf("Nginx returned code %v\n", statusCode)
return
}

Expand All @@ -171,14 +190,33 @@ func backendsGet(name string) {
fmt.Println("A backend of this name was not found.")
}

func certGet(host string) {
statusCode, body, requestErr := nginx.NewGetStatusRequest(certsPath + "?hostname=" + host)
if requestErr != nil {
fmt.Println(requestErr)
return
}

if statusCode == 200 {
fmt.Print(string(body))
return
} else if statusCode != 404 {
fmt.Printf("Nginx returned code %v\n", statusCode)
fmt.Println(string(body))
return
}

fmt.Printf("No cert found for host %v\n", host)
}

func general() {
statusCode, body, requestErr := nginx.NewGetStatusRequest(generalPath)
if requestErr != nil {
fmt.Println(requestErr)
return
}
if statusCode != 200 {
fmt.Printf("Nginx returned code %v", statusCode)
fmt.Printf("Nginx returned code %v\n", statusCode)
return
}

Expand Down
31 changes: 28 additions & 3 deletions internal/nginx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io/ioutil"
"net/http"
"os"
"strings"
"time"

"github.com/tv42/httpunix"
Expand Down Expand Up @@ -88,15 +89,39 @@ func NewPostStatusRequest(path, contentType string, data interface{}) (int, []by
return res.StatusCode, body, nil
}

// GetServerBlock takes an nginx.conf file and a host and tries to find the server block for that host
func GetServerBlock(conf string, host string) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you leave this here because it'll be needed later on when we add more commands to dbg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. I'm also making use of it in the actual plugin, and I feel that it's good to keep it next to the other common nginx.conf reading stuff.

startMsg := fmt.Sprintf("## start server %v", host)
endMsg := fmt.Sprintf("## end server %v", host)

blockStart := strings.Index(conf, startMsg)
if blockStart < 0 {
return "", fmt.Errorf("Host %v was not found in the controller's nginx.conf", host)
}
blockStart = blockStart + len(startMsg)

blockEnd := strings.Index(conf, endMsg)
if blockEnd < 0 {
return "", fmt.Errorf("The end of the host server block could not be found, but the beginning was")
}

return conf[blockStart:blockEnd], nil
}

// ReadNginxConf reads the nginx configuration file into a string
func ReadNginxConf() (string, error) {
confFile, err := os.Open("/etc/nginx/nginx.conf")
return ReadFileToString("/etc/nginx/nginx.conf")
}

// ReadFileToString reads any file into a string
func ReadFileToString(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer confFile.Close()
defer f.Close()

contents, err := ioutil.ReadAll(confFile)
contents, err := ioutil.ReadAll(f)
if err != nil {
return "", err
}
Expand Down
31 changes: 31 additions & 0 deletions rootfs/etc/nginx/lua/configuration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,32 @@ local function handle_general()
ngx.status = ngx.HTTP_CREATED
end

local function handle_certs()
if ngx.var.request_method ~= "GET" then
ngx.status = ngx.HTTP_BAD_REQUEST
ngx.print("Only GET requests are allowed!")
return
end

local query = ngx.req.get_uri_args()
if not query["hostname"] then
ngx.status = ngx.HTTP_BAD_REQUEST
ngx.print("Hostname must be specified.")
return
end

local key = _M.get_pem_cert_key(query["hostname"])
if key then
ngx.status = ngx.HTTP_OK
ngx.print(key)
return
else
ngx.status = ngx.HTTP_NOT_FOUND
ngx.print("No key associated with this hostname.")
return
end
end

function _M.call()
if ngx.var.request_method ~= "POST" and ngx.var.request_method ~= "GET" then
ngx.status = ngx.HTTP_BAD_REQUEST
Expand All @@ -127,6 +153,11 @@ function _M.call()
return
end

if ngx.var.uri == "/configuration/certs" then
handle_certs()
return
end

if ngx.var.request_uri ~= "/configuration/backends" then
ngx.status = ngx.HTTP_NOT_FOUND
ngx.print("Not found!")
Expand Down