| 1 |
package cautils |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"io" |
| 6 |
"net/http" |
| 7 |
"net/url" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/smallstep/certificates/pki" |
| 11 |
"github.com/smallstep/cli/exec" |
| 12 |
"github.com/urfave/cli" |
| 13 |
|
| 14 |
"github.com/pkg/errors" |
| 15 |
) |
| 16 |
|
| 17 |
type bootstrapAPIResponse struct { |
| 18 |
CaURL string `json:"url"` |
| 19 |
Fingerprint string `json:"fingerprint"` |
| 20 |
RedirectURL string `json:"redirect-url"` |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
func BootstrapTeam(ctx *cli.Context, teamID string) error { |
| 26 |
apiEndpoint := ctx.String("team-url") |
| 27 |
if apiEndpoint == "" { |
| 28 |
|
| 29 |
u := url.URL{ |
| 30 |
Scheme: "https", |
| 31 |
Host: "api.smallstep.com", |
| 32 |
Path: "/v1/teams/" + teamID + "/authorities/ssh", |
| 33 |
} |
| 34 |
apiEndpoint = u.String() |
| 35 |
} else { |
| 36 |
|
| 37 |
apiEndpoint = strings.ReplaceAll(apiEndpoint, "<>", teamID) |
| 38 |
u, err := url.Parse(apiEndpoint) |
| 39 |
if err != nil { |
| 40 |
return errors.Wrapf(err, "error parsing %s", apiEndpoint) |
| 41 |
} |
| 42 |
apiEndpoint = u.String() |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
resp, err := http.Get(apiEndpoint) |
| 47 |
if err != nil { |
| 48 |
return errors.Wrap(err, "error getting team data") |
| 49 |
} |
| 50 |
if resp.StatusCode >= 400 { |
| 51 |
if resp.StatusCode == http.StatusNotFound { |
| 52 |
return errors.New("error getting team data: team not found") |
| 53 |
} |
| 54 |
return errors.Wrap(readError(resp.Body), "error getting team data") |
| 55 |
} |
| 56 |
|
| 57 |
var r bootstrapAPIResponse |
| 58 |
if err := readJSON(resp.Body, &r); err != nil { |
| 59 |
return errors.Wrap(err, "error getting team data") |
| 60 |
} |
| 61 |
|
| 62 |
if r.RedirectURL == "" { |
| 63 |
r.RedirectURL = "https://smallstep.com/app/teams/sso/success" |
| 64 |
} |
| 65 |
|
| 66 |
args := []string{"ca", "bootstrap", |
| 67 |
"--ca-url", r.CaURL, |
| 68 |
"--fingerprint", r.Fingerprint, |
| 69 |
"--redirect-url", r.RedirectURL, |
| 70 |
} |
| 71 |
if ctx.Bool("force") { |
| 72 |
args = append(args, "--force") |
| 73 |
} |
| 74 |
if _, err := exec.Step(args...); err != nil { |
| 75 |
return errors.Wrap(err, "error getting team data") |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
ctx.Set("ca-url", r.CaURL) |
| 80 |
ctx.Set("fingerprint", r.Fingerprint) |
| 81 |
ctx.Set("root", pki.GetRootCAPath()) |
| 82 |
|
| 83 |
return nil |
| 84 |
} |
| 85 |
|
| 86 |
type apiError struct { |
| 87 |
StatusCode int `json:"statusCode"` |
| 88 |
Err string `json:"error"` |
| 89 |
Message string `json:"message"` |
| 90 |
} |
| 91 |
|
| 92 |
func (e *apiError) Error() string { |
| 93 |
return e.Message |
| 94 |
} |
| 95 |
|
| 96 |
func readJSON(r io.ReadCloser, v interface{}) error { |
| 97 |
defer r.Close() |
| 98 |
if err := json.NewDecoder(r).Decode(v); err != nil { |
| 99 |
return err |
| 100 |
} |
| 101 |
return nil |
| 102 |
} |
| 103 |
|
| 104 |
func readError(r io.ReadCloser) error { |
| 105 |
defer r.Close() |
| 106 |
apiErr := new(apiError) |
| 107 |
if err := json.NewDecoder(r).Decode(apiErr); err != nil { |
| 108 |
return err |
| 109 |
} |
| 110 |
return apiErr |
| 111 |
} |