2015-06-26 01:47:32 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-06-30 02:00:08 +00:00
|
|
|
"strings"
|
2015-06-26 01:47:32 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
func pathLookup(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "lookup",
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"ip": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "IP address of target",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.WriteOperation: b.pathLookupWrite,
|
|
|
|
},
|
|
|
|
HelpSynopsis: pathLookupSyn,
|
|
|
|
HelpDescription: pathLookupDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathLookupWrite(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-06-30 20:30:13 +00:00
|
|
|
ipAddr := d.Get("ip").(string)
|
|
|
|
if ipAddr == "" {
|
|
|
|
return logical.ErrorResponse("Missing 'ip'"), nil
|
2015-06-26 01:47:32 +00:00
|
|
|
}
|
2015-06-30 20:30:13 +00:00
|
|
|
ip := net.ParseIP(ipAddr)
|
|
|
|
if ip == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Invalid IP '%s'", ip.String())), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
keys, err := req.Storage.List("policy/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(keys) == 0 {
|
|
|
|
return nil, fmt.Errorf("No roles registered for IP '%s'", ip.String())
|
|
|
|
}
|
|
|
|
|
2015-06-26 01:47:32 +00:00
|
|
|
var matchingRoles []string
|
|
|
|
for _, item := range keys {
|
2015-06-30 20:30:13 +00:00
|
|
|
if contains, _ := containsIP(req.Storage, item, ip.String()); contains {
|
2015-06-26 01:47:32 +00:00
|
|
|
matchingRoles = append(matchingRoles, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"roles": matchingRoles,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func containsIP(s logical.Storage, roleName string, ip string) (bool, error) {
|
2015-06-30 20:30:13 +00:00
|
|
|
if roleName == "" || ip == "" {
|
|
|
|
return false, fmt.Errorf("invalid parameters")
|
|
|
|
}
|
2015-06-26 01:47:32 +00:00
|
|
|
roleEntry, err := s.Get("policy/" + roleName)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error retrieving role '%s'", err)
|
|
|
|
}
|
|
|
|
if roleEntry == nil {
|
|
|
|
return false, fmt.Errorf("role '%s' not found", roleName)
|
|
|
|
}
|
|
|
|
var role sshRole
|
|
|
|
if err := roleEntry.DecodeJSON(&role); err != nil {
|
|
|
|
return false, fmt.Errorf("error decoding role '%s'", roleName)
|
|
|
|
}
|
|
|
|
ipMatched := false
|
2015-06-30 02:00:08 +00:00
|
|
|
for _, item := range strings.Split(role.CIDR, ",") {
|
2015-06-30 20:30:13 +00:00
|
|
|
_, cidrIPNet, err := net.ParseCIDR(item)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf(fmt.Sprintf("Invalid cidr entry '%s'", item))
|
|
|
|
}
|
2015-06-26 01:47:32 +00:00
|
|
|
ipMatched = cidrIPNet.Contains(net.ParseIP(ip))
|
|
|
|
if ipMatched {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ipMatched, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathLookupSyn = `
|
2015-06-30 20:30:13 +00:00
|
|
|
Lists 'roles' that can be used to create a dynamic key.
|
2015-06-26 01:47:32 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const pathLookupDesc = `
|
2015-06-30 20:30:13 +00:00
|
|
|
CIDR blocks will be associated with multiple 'roles'.
|
|
|
|
This endpoint lists all the 'roles' that are associated with the supplied IP address.
|
2015-06-26 01:47:32 +00:00
|
|
|
`
|