aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorB Stack <bgstack15@gmail.com>2019-03-07 13:56:06 -0500
committerB Stack <bgstack15@gmail.com>2019-03-07 13:56:06 -0500
commitccc2e586815842342c35c6fc5c61ef1c2dd3eba2 (patch)
tree54f9fb6b1ff986b793b2d30732aff6d35af334c7
parentMerge branch 'add-san' into 'master' (diff)
downloadcertreq-rest-api-go.tar.gz
certreq-rest-api-go.tar.bz2
certreq-rest-api-go.zip
WIP: add initial work for rest apirest-api-go
-rw-r--r--crg2.go131
1 files changed, 131 insertions, 0 deletions
diff --git a/crg2.go b/crg2.go
new file mode 100644
index 0000000..41f7c13
--- /dev/null
+++ b/crg2.go
@@ -0,0 +1,131 @@
+// 2019-03-04 16:34 this is better than the previous one, although very clunky. Need to make a new "type Item struct { }" where Item is the object to be changed when you want to have a new object. But the functions should be a minimal example to show how to deal with POST, GET.
+// Reference
+// docs https://godoc.org/github.com/dougblack/sleepy
+// https://github.com/dougblack/sleepy as gitlab.com/bgstack15/sleepy
+// https://gobyexample.com/json
+package main
+
+import (
+ "net/url"
+ "net/http"
+ //"github.com/dougblack/sleepy"
+ "gitlab.com/bgstack15/sleepy"
+ "fmt"
+ "encoding/json"
+ "strconv"
+ "os/exec"
+ "io/ioutil"
+)
+
+type Item struct{}
+type Harbor struct{}
+type CertreqResponse struct{
+ Csrfile string `json:"csrfile"`
+ Certificatefile string `json:"certificatefile"`
+ Keyfile string `json:"keyfile"`
+ Rc int `json:"rc"`
+}
+
+type CertreqContents struct{
+ Csr string `json:"csr"`
+ Cert string `json:"cert"`
+ Key string `json:"key"`
+ Rc int `json:"rc"`
+ // add chain?
+}
+
+type CertreqInput struct{
+ Ca string `json:"ca"`
+ Pass string `json:"pass"`
+ Subject string `json:"subject"`
+ Template string `json:"template"`
+ User string `json:"user"`
+}
+
+// what is passed in, name, and return values
+func Certreq_Request(i CertreqInput) (int, CertreqResponse) {
+ var o CertreqResponse
+ // WORKHERE figure how to sent environment variables for the command.
+ crexec := exec.Command("/home/bgstack15/dev/certreq/files/certreq.sh","-c","/home/bgstack15/dev/certreq/files/certreq.conf")
+ //execIn, _ := crexec.StdinPipe()
+ execOut, _ := crexec.StdoutPipe()
+ crexec.Start()
+ crexecBytes, _ := ioutil.ReadAll(execOut)
+ crexec.Wait()
+ fmt.Print(string(crexecBytes))
+
+ o.Rc = 5
+ o.Keyfile = "something"
+ return 0, o
+}
+
+func (certreqweb CertreqInput) Get(values url.Values, headers http.Header) (int, interface{}, http.Header) {
+ var i CertreqInput
+ var e int = 200
+ var r string
+ for {
+ if values.Get("subject") == "" {
+ e = 400
+ r = "subject is not defined"
+ break
+ } else { i.Subject = values.Get("subject") }
+ if values.Get("template") == "" {
+ fmt.Print("using default template...\n")
+ } else { i.Template = values.Get("template") }
+ if values.Get("ca") == "" {
+ fmt.Print("using default ca...\n")
+ } else { i.Ca = values.Get("ca") }
+ break
+ }
+
+ // check for errors
+ if ( e != 200 ) {
+ return e, map[string][]string{"errorcode":[]string{strconv.Itoa(e)},"reason":[]string{r}}, http.Header{"Content-type:": {"application/json"}}
+ }
+ // after all the checks, we should be good to go
+
+ data , _ := json.Marshal(&map[string]interface{}{"input": i})
+ var out1, out2 map[string]interface{}
+ json.Unmarshal(data,&out1)
+
+ // call function and pass in i
+ _ , o := Certreq_Request(i)
+ data , _ = json.Marshal(&map[string]interface{}{"output": o})
+ json.Unmarshal(data,&out2)
+
+ // debugging output and return values
+ fmt.Print("CertreqWeb: ",values,"\n")
+ return e, out2, http.Header{"Content-type": {"application/json"}}
+}
+
+func (item Item) Post(values url.Values, headers http.Header) (int, interface{}, http.Header) {
+ items := []string{"please replace", "with error"}
+ data := map[string][]string{"items": items}
+ return 200, data, http.Header{"Content-type": {"application/json"}}
+}
+
+func (harbor Harbor) Get(values url.Values, headers http.Header) (int, interface{}, http.Header) {
+ items := []string{"item1", "HARBORMONSTER"}
+ data := map[string][]string{"items": items}
+ fmt.Print(values)
+ return 200, data, http.Header{"Content-type": {"application/json"}}
+}
+
+func (harbor Harbor) Post(values url.Values, headers http.Header) (int, interface{}, http.Header) {
+ items := []string{"item1", "posterized"}
+ data := map[string][]string{"items": items}
+ fmt.Print(values)
+ return 200, data, http.Header{"Content-type": {"application/json"}}
+}
+
+func main() {
+ //item := new(Item)
+ //harbor := new(Harbor)
+
+ api := sleepy.NewAPI()
+ api.AddResource(new(Item), "/items")
+ api.AddResource(new(Harbor), "/harborfreight")
+ api.AddResource(new(CertreqInput), "/v1/certreq")
+ //api.AddResourceWithWrapper(item, Special, "/item")
+ api.StartTLS(8081,"/home/bgstack15/dev/c1/gocert.crt","/home/bgstack15/dev/c1/gocert.key")
+}
bgstack15