go Packed binary and lost permission to run on server - go

Steps:
go build . /main.go
./main # run
curl localhost:8080 # app run on 8080
The app exits with non-removable permissions attached
step image
I'm developing a go app for the first time if there is anything else I need to provide I will update the content as soon as possible, thanks
Because curl's is an unmatched route, we won't post the contoller code here
package main
import (
"github.com/gin-gonic/gin"
"example.app/common"
"example.app/config"
"example.app/controller"
"example.app/db"
"example.app/middleware"
)
func main() {
config.Initialize()
gin.SetMode(config.AppMode)
r := gin.Default()
r.SetTrustedProxies([]string{"::"})
// setup auth middleware.
r.Use(middleware.Auth())
common.InitValidator()
// setup routes.
controller.Initialize(r)
r.Run(":" + config.AppPort)
}
go mode code
env
Linux instance-220907111205 4.15.0-22-generic #24-Ubuntu SMP Wed May 16 12:15:17 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Related

How to fix Golang nw_path_close_fd Failed to close guarded necp fd 6 [9: Bad file descriptor]

Hello I have a simple Go app running on macOS Mojave 10.14.4 with go version go1.15 darwin/amd64
import (
"net/http"
"os/exec"
)
func main() {
_, _ = http.Get("https://stackoverflow.com/")
exec.Command("ls").Start()
}
It runs without any errors but if you run it while having the Console app you will see the following error logged from /usr/lib/libnetwork.dylib:
nw_path_close_fd Failed to close guarded necp fd 6 [9: Bad file descriptor]
The error gets triggered by the line exec.Command("ls").Start(), you can use a breakpoint or put it inside a loop to see it occur every time that line executes. Anyone knows what could be causing this and how to fix it?
Note: it does not happen with all URLs, for example https://www.facebook.com does not trigger the error.
Update: An additional detail, the error also doesn't show if I compile that app with go1.11.13

How to run a Go function with redis package on OpenWhisk?

I am having some troubles to run a golang package on OpenWhisk (IBM Cloud Functions).
I ran the following on my local computer and it works without any problems (go run sample.go):
package main
import (
"fmt"
"encoding/json"
"github.com/go-redis/redis"
)
func main() {
var redisClient *redis.Client = redis.NewClient(&redis.Options{
Addr: "...",
Password: "...",
DB: 0,
})
redisClient.Set("foo", "bar", 0)
defer redisClient.Close()
msg := map[string]string{"msg": ("Done !")}
res, _ := json.Marshal(msg)
fmt.Println(string(res))
}
But i didn't find any way to make it working on OpenWhisk. I ran the following:
GOOS=linux GOARCH=amd64 go build -o exec sample.go
zip exec.zip exec
bx wsk action update myfunction --native exec.zip
bx wsk action invoke myfunction -r
bx wsk activation logs --last --strip
"error": "The action did not return a dictionary."
"2018-02-21T01:21:05.962244788Z stdout: [Errno 2] No such file or
directory: '/action/exec'"
The problem is related to the github.com/go-redis/redis package, when i remove it and its code then the function is running well. I met the same problem with the mgo package (MongoDB)...
I am new in Golang so it may be obvious, but for now i am stuck :/
The binary in the zip file is dynamically linked against shared libraries not available on the platform.
Using file and ldd confirms this:
$ file exec
exec: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, not stripped
$ ldd exec
/lib64/ld-linux-x86-64.so.2 (0x7f3f63f10000)
libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7f3f63f10000)
libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7f3f63f10000)
Build a static binary or build a dynamically linked binary inside the Docker image used by the platform (openwhisk/dockerskeleton).
The file not found error is misleading but reported by bash when executing files with this issue.
"error": "The action did not return a dictionary."
This is an open wsk error message.
It means that you did not return a dictionary on stdout
below is a valid return as it returns a json object
{"error":"something broke!"}
below would be invalid
"something broke!"
suggestions: check to see if there is any errors when you build ( normally )
the file in your dropbox doesnt look like a binary file...I suggest checking you can build the binary first

How can a golang error reporting function get compilation information

How can a function establish the following details from the runtime so that it can craft appropriate error reporting as part of a error library to be used using in many products (we are changing over to golang):
Compilation date and time of the executable
Compilation machine used to create the executable
I would like to be able to retrieve both of these to augment the various file revision numbers that I can report along with some stack trace information
Useful related but off-topic information:
- You can get stack traces out of the runtime as illustrated here http://technosophos.com/2014/03/19/generating-stack-traces-in-go.html
- The reflect package http://golang.org/pkg/reflect/ can be used to examine an identified function
Thank you for your assistance,
Richard
You can use the -X linker flag to set the value of a string variable when building:
go build -ldflags "-X main.Uname '$(uname -a)' -X main.CompileTime '$(date)'"
With such command, this code
package main
import "fmt"
// Set by the linker.
var CompileTime, Uname string
func main() {
fmt.Println(Uname)
fmt.Println(CompileTime)
}
will print something like
Linux user 3.13.0-53 Wed May 20 10:34:39 UTC 2015 x86_64 GNU/Linux
Wed May 27 12:00:00 UTC 2015
See the linker docs for more info.

Cross compile net/http for distribution

I have build the following code in a docker container with the following architecture:
cat /proc/version
Linux version 3.16.7-tinycore64 (root#064f0e1ce709) (gcc version 4.7.2 (Debian 4.7.2-5) ) #1 SMP Tue Dec 16 23:03:39 UTC 2014
package main
import "fmt"
func main() {
fmt.Println("Hello")
}
The binary distributed, runs with no problem on a busybox container, with the same architecture without installing golang.
The problem
When I do the same for the following code:
package main
import (
"fmt"
"net/http"
)
const (
port = ":80"
)
var calls = 0
func HelloWorld(w http.ResponseWriter, r *http.Request) {
calls++
fmt.Fprintf(w, "Hello, world! You have called me %d times.\n", calls)
}
func init() {
fmt.Printf("Started server at http://localhost%v.\n", port)
http.HandleFunc("/", HelloWorld)
http.ListenAndServe(port, nil)
}
func main() {}
Then I get:
ash: ./hello_world: not found
I might be missing some dependencies - like "net/http"?
But I thought the go build would build all into the binaries.
This is for both go build & go install.
Any idea?
The answer is most probably the one described in this article.
Some critical parts of the standard library use CGO [...] if you cross-compile Go to Darwin or Linux your programs won’t use the system DNS resolver. They also can’t use the native host certificate store. They also can’t look up the user’s home directory, either.
And CGO links against some standard system interfaces by default, dynamically.
The article suggests using gonative to fix the problem. If that's not your cup of tea, some people suggest using:
go build -ldflags "-linkmode external -extldflags -static"
Also read: https://groups.google.com/d/topic/golang-nuts/H-NTwhQVp-8/discussion
I think you need to disable cgo and build with netgo flag :
The net package requires cgo by default because the host operating
system must in general mediate network call setup. On some systems,
though, it is possible to use the network without cgo, and useful to
do so, for instance to avoid dynamic linking. The new build tag netgo
(off by default) allows the construction of a net package in pure Go
on those systems where it is possible.
The netgo tag requires version 1.2 and above.

Updating go websocket library to latest version

I am running the Go compiler on Ubuntu, installed using sudo apt-get install golang
I've successfully compiled and executed the code for a Trivial example server (See http://golang.org/pkg/websocket/#Handler )
package main
import (
"http"
"io"
"websocket"
)
// Echo the data received on the Web Socket.
func EchoServer(ws *websocket.Conn) {
io.Copy(ws, ws);
}
func main() {
http.Handle("/echo", websocket.Handler(EchoServer));
err := http.ListenAndServe(":12345", nil);
if err != nil {
panic("ListenAndServe: " + err.String())
}
}
However, I fail to connect to the server with my version of Chromium (16.0.912.77). I assume Chrome has implemented the RFC 6455 Websocket (version 13), but that the go websocket library in the Ubuntu golang package is out of date.
So, my question is: How can I update only the websocket package to the latest version?
The latest version of the Go websocket package is net/websocket at code.google.com/p/go.net/websocket, which requires the Go 1 weekly development release.
For Ubuntu golang-weekly: Ubuntu PPA packages for Go.
For weekly development release documentation: Go Programming Language.
I guess the version of Go in Ubuntu package repository is probably r60.3 (or so), which is a bit old now. Use latest weekly, change the code to:
package main
import (
"code.google.com/p/go.net/websocket"
"io"
"net/http"
)
// Echo the data received on the Web Socket.
func EchoServer(ws *websocket.Conn) {
io.Copy(ws, ws)
}
func main() {
http.Handle("/echo", websocket.Handler(EchoServer))
err := http.ListenAndServe(":12345", nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}
Moreover in the websocket package s/ParseRequestURI/ParseRequest/, then it seems to work here.(1)
Update: Sorry, I wrote/read too fast, it doesn't seem to work, the page shows: "not websocket protocol" (here is Chrome 18.0.1025.33 beta on 64b Ubuntu 10.04)
Update 2012-08-22: The above (1) note about editing the websocket package doesn't hold anymore. The websocket package has been meanwhile updated and the example (main) code above now compiles w/o problems. Anyway, I haven't tested if it afterwards does what is should or not, sorry.

Resources