{"id":1720,"date":"2021-03-22T08:16:58","date_gmt":"2021-03-22T13:16:58","guid":{"rendered":"https:\/\/blog.iqonda.net\/?p=1720"},"modified":"2021-03-22T08:16:58","modified_gmt":"2021-03-22T13:16:58","slug":"terraform-stateserver-using-go","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/terraform-stateserver-using-go\/","title":{"rendered":"Terraform Stateserver Using Go"},"content":{"rendered":"

Terraform can utilize a http backend for maintaining state. This is a test of a Terraform http backend using a server implemented with go.<\/p>\n

recipe and components<\/h2>\n
\u2022 Using Virtualbox Ubuntu 20.10 and links listed below\n\u2022 Run as http server\n\u2022 Change to https\n\u2022 For a future test terraform http backend locking feature and\/or include locking in server<\/code><\/pre>\n

links<\/h2>\n
\u2022 https:\/\/www.terraform.io\/docs\/language\/settings\/backends\/http.html\n\u2022 https:\/\/linuxhint.com\/install-golang-ubuntu\/\n\u2022 https:\/\/github.com\/MerlinDMC\/go-terraform-stateserver\n\u2022 https:\/\/github.com\/denji\/golang-tls<\/code><\/pre>\n

setup go and test<\/h2>\n
# go version\ngo version go1.14.7 linux\/amd64\n\n$ pwd\n\/home\/rrosso\/tf-go-server<\/code><\/pre>\n
$ cat main.go \npackage main\nimport \"fmt\"\nfunc main() {\nfmt.Println(\"Hello World\");\n}<\/code><\/pre>\n
$ go run main.go \nHello World\n\n$ go build main.go \n$ .\/main \nHello World<\/code><\/pre>\n

run server and test response<\/h2>\n
$ go run tf-stateserver.go -data_path=\/home\/rrosso\/tf-go-server -listen_address=192.168.1.235:8080\n\n$ curl -sL http:\/\/192.168.1.235:8080 | xxd\n00000000: 3430 3420 7061 6765 206e 6f74 2066 6f75  404 page not fou\n00000010: 6e64 0and.<\/code><\/pre>\n

terraform init with http backend<\/h2>\n
\u279c  terraform-poc tail -8 main.tf \n#}\n## POC to test a go server\nterraform {\n  backend \"http\" {\n    address = \"http:\/\/192.168.1.235:8080\/states\/terraform.tfstate\"\n  }\n}\n\n\u279c  terraform-poc terraform init\n\nInitializing the backend...\nDo you want to copy existing state to the new backend?\n  Pre-existing state was found while migrating the previous \"local\" backend to the\n  newly configured \"http\" backend. No existing state was found in the newly\n  configured \"http\" backend. Do you want to copy this state to the new \"http\"\n  backend? Enter \"yes\" to copy and \"no\" to start with an empty state.\n\n  Enter a value: yes\n\nSuccessfully configured the backend \"http\"! Terraform will automatically\nuse this backend unless the backend configuration changes.\n\nInitializing provider plugins...\n\nThe following providers do not have any version constraints in configuration,\nso the latest version was installed.\n\nTo prevent automatic upgrades to new major versions that may contain breaking\nchanges, it is recommended to add version = \"...\" constraints to the\ncorresponding provider blocks in configuration, with the constraint strings\nsuggested below.\n\n* provider.oci: version = \"~> 4.17\"\n\nTerraform has been successfully initialized!\n\nYou may now begin working with Terraform. Try running \"terraform plan\" to see\nany changes that are required for your infrastructure. All Terraform commands\nshould now work.\n\nIf you ever set or change modules or backend configuration for Terraform,\nrerun this command to reinitialize your working directory. If you forget, other\ncommands will detect it and remind you to do so if necessary.<\/code><\/pre>\n

observe server log messages<\/h2>\n
\u2022 file not found if no file exist during init is expected\n\u2022 if file same and already there GET fine during init\n\u2022 plan does a GET\n\u2022 apply does a POST\n\u2022 did not observe a DELETE<\/code><\/pre>\n
$ go run tf-stateserver.go -data_path=\/home\/rrosso\/tf-go-server -listen_address=192.168.1.235:8080\n2021\/03\/18 09:57:41 received GET for state file: \/home\/rrosso\/tf-go-server\/states\/terraform.tfstate\n...\n2021\/03\/18 09:58:33 received POST for state file: \/home\/rrosso\/tf-go-server\/states\/terraform.tfstate\n...\n2021\/03\/18 10:00:50 received GET for state file: \/home\/rrosso\/tf-go-server\/states\/terraform.tfstate\n2021\/03\/18 10:00:50 during GET cannot open file: open \/home\/rrosso\/tf-go-server\/states\/terraform.tfstate: no such file or directory\n2021\/03\/18 10:00:50 received GET for state file: \/home\/rrosso\/tf-go-server\/states\/terraform.tfstate<\/code><\/pre>\n

change to self signed cert for https test<\/h2>\n

NOTE:
\n\u2022 use sudo since linux instance is not running low ports like 443 for normal users
\n\u2022 need terraform skip_cert_verification for this self signed certificate<\/p>\n

$ openssl genrsa -out server.key 2048\nGenerating RSA private key, 2048 bit long modulus (2 primes)\n..........................+++++\n.......................................................+++++\ne is 65537 (0x010001)\n\n$ openssl ecparam -genkey -name secp384r1 -out server.key\n\n$ openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650\n...\n\n$ sudo go run tf-stateserver.go -certfile=\"server.crt\" -keyfile=\"server.key\" -data_path=.\/ -listen_address=192.168.1.235:443\n\n$ curl -sL http:\/\/192.168.1.235:443 | xxd\n00000000: 436c 6965 6e74 2073 656e 7420 616e 2048  Client sent an H\n00000010: 5454 5020 7265 7175 6573 7420 746f 2061  TTP request to a\n00000020: 6e20 4854 5450 5320 7365 7276 6572 2e0a  n HTTPS server..\n\n\u279c  terraform-poc tail -8 main.tf \n#}\n## POC to test a go server\nterraform {\n  backend \"http\" {\n    address = \"https:\/\/192.168.1.235\/states\/terraform.tfstate\"\n    skip_cert_verification = true\n  }\n}<\/code><\/pre>\n

NOTE: terraform init, plan, apply works<\/p>\n

additional links<\/h2>\n
\u2022 https:\/\/appdividend.com\/2019\/11\/29\/golang-json-example-how-to-use-json-with-go\/\n\u2022 https:\/\/www.golangprograms.com\/web-application-to-read-and-write-json-data-in-json-file.html\n\u2022 https:\/\/tutorialedge.net\/golang\/parsing-json-with-golang\/\n\u2022 https:\/\/dzone.com\/articles\/how-to-write-a-http-rest-api-server-in-go-in-minut<\/code><\/pre>\n

source<\/h2>\n

NOTE: subsequently checked source into github here: https:\/\/github.com\/rrossouw01\/terraform-stateserver-go\/blob\/main\/server.go<\/a> <\/p>\n

Can use the file directly with simple wget to the raw link for example:<\/p>\n

$ wget https:\/\/raw.githubusercontent.com\/rrossouw01\/terraform-stateserver-go\/main\/server.go<\/code><\/pre>\n
$ cat tf-stateserver.go \npackage main\n\/\/https:\/\/github.com\/MerlinDMC\/go-terraform-stateserver\/blob\/master\/main.go\n\nimport (\n    \"flag\"\n    \"io\"\n    \"log\"\n    \"net\/http\"\n    \"os\"\n    \"path\/filepath\"\n)\n\nvar (\n    flagDataPath string\n    flagCertFile string\n    flagKeyFile  string\n\n    flagListenAddress string\n)\n\nfunc init() {\n    flag.StringVar(&flagCertFile, \"certfile\", \"\", \"path to the certs file for https\")\n    flag.StringVar(&flagKeyFile, \"keyfile\", \"\", \"path to the keyfile for https\")\n    flag.StringVar(&flagDataPath, \"data_path\", os.TempDir(), \"path to the data storage directory\")\n\n    flag.StringVar(&flagListenAddress, \"listen_address\", \"0.0.0.0:8080\", \"address:port to bind listener on\")\n    \/\/flag.StringVar(&flagListenAddress, \"listen_address\", \"192.168.1.235:8080\", \"address:port to bind listener on\")\n}\n\nfunc main() {\n    if !flag.Parsed() {\n        flag.Parse()\n    }\n\n    router := http.NewServeMux()\n    router.HandleFunc(\"\/\", requestHandler)\n\n    if flagCertFile != \"\" && flagKeyFile != \"\" {\n          log.Fatal(http.ListenAndServeTLS(flagListenAddress, flagCertFile, flagKeyFile, router))\n    } else {\n          log.Fatal(http.ListenAndServe(flagListenAddress, router))\n    }\n}\n\nfunc requestHandler(res http.ResponseWriter, req *http.Request) {\n    if req.URL.Path == \"\/\" {\n        http.NotFound(res, req)\n        return\n    }\n\n    stateStorageFile := filepath.Join(flagDataPath, req.URL.Path)\n    stateStorageDir := filepath.Dir(stateStorageFile)\n\n    switch req.Method {\n    case \"GET\":\n        log.Printf(\"received GET for state file: %s\\n\", stateStorageFile)\n        fh, err := os.Open(stateStorageFile)\n        if err != nil {\n            log.Printf(\"during GET cannot open file: %s\\n\", err)\n            goto not_found\n        }\n        defer fh.Close()\n\n        res.WriteHeader(200)\n\n        io.Copy(res, fh)\n\n        return\n    case \"POST\":\n        log.Printf(\"received POST for state file: %s\\n\", stateStorageFile)\n        if err := os.MkdirAll(stateStorageDir, 0750); err != nil && !os.IsExist(err) {\n            log.Printf(\"during POST cannot create parent directories: %s\\n\", err)\n            goto not_found\n        }\n\n        fh, err := os.OpenFile(stateStorageFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)\n        if err != nil {\n            log.Printf(\"cannot open file: %s\\n\", err)\n            goto not_found\n        }\n        defer fh.Close()\n\n        if _, err := io.Copy(fh, req.Body); err != nil {\n            log.Printf(\"failed to stream data into statefile: %s\\n\", err)\n            goto not_found\n        }\n\n        res.WriteHeader(200)\n\n        return\n    case \"DELETE\":\n        log.Printf(\"received DELETE for state file: %s\\n\", stateStorageFile)\n        \/\/if os.RemoveAll(stateStorageFile) != nil {\n        if err := os.RemoveAll(stateStorageFile); err != nil {\n            log.Printf(\"cannot remove file: %s\\n\", err)\n            goto not_found\n        }\n\n        res.WriteHeader(200)\n\n        return\n    }\n\nnot_found:\n    http.NotFound(res, req)\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

Terraform can utilize a http backend for maintaining state. This is a test of a Terraform http backend using a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101,88],"tags":[],"class_list":["post-1720","post","type-post","status-publish","format-standard","hentry","category-go","category-terraform"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=1720"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1720\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}