generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Daichi Sakaue <[email protected]>
- Loading branch information
Showing
9 changed files
with
181 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Build the manager binary | ||
FROM ghcr.io/cybozu/golang:1.23-jammy AS builder | ||
|
||
# Copy the Go Modules manifests | ||
COPY go.mod go.mod | ||
COPY go.sum go.sum | ||
# cache deps before building and copying source so that we don't need to re-download as much | ||
# and so that source changes don't invalidate our downloaded layer | ||
RUN go mod download | ||
|
||
# Copy the go source | ||
COPY cmd/cilium-agent-proxy/ cmd/cilium-agent-proxy/ | ||
|
||
# Build | ||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o cilium-agent-proxy ./cmd/cilium-agent-proxy | ||
|
||
# Compose the manager container | ||
FROM ghcr.io/cybozu/ubuntu:22.04 | ||
LABEL org.opencontainers.image.source=https://github.com/cybozu-go/network-policy-viewer | ||
|
||
WORKDIR / | ||
COPY --from=builder /work/cilium-agent-proxy / | ||
|
||
USER 10000:10000 | ||
ENTRYPOINT ["/cilium-agent-proxy"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "github.com/cybozu-go/network-policy-viewer/cmd/cilium-agent-proxy/sub" | ||
|
||
func main() { | ||
sub.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package sub | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "cilium-agent-proxy", | ||
Short: "cilium-agent proxy", | ||
Long: `cilium-agent proxy`, | ||
|
||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cmd.SilenceUsage = true | ||
return subMain() | ||
}, | ||
} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package sub | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
const socketPath = "/var/run/cilium/cilium.sock" | ||
|
||
var ( | ||
endpointPattern *regexp.Regexp | ||
identityPattern *regexp.Regexp | ||
) | ||
|
||
func handleEndpoint(w http.ResponseWriter, r *http.Request) { | ||
match := endpointPattern.FindStringSubmatch(r.URL.Path) | ||
if match == nil || len(match) < 2 { | ||
fmt.Fprint(w, "error\n") | ||
return | ||
} | ||
client := http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { | ||
return net.Dial("unix", socketPath) | ||
}, | ||
}, | ||
} | ||
|
||
// Convert to number to avoid possible parameter injection | ||
endpoint, err := strconv.Atoi(match[1]) | ||
if err != nil { | ||
fmt.Fprint(w, "error\n") | ||
return | ||
} | ||
|
||
url := fmt.Sprintf("http://localhost/v1/endpoint/%d", endpoint) | ||
resp, err := client.Get(url) | ||
if err != nil { | ||
fmt.Fprint(w, "error\n") | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
io.Copy(w, resp.Body) | ||
} | ||
|
||
func handleIdentity(w http.ResponseWriter, r *http.Request) { | ||
match := identityPattern.FindStringSubmatch(r.URL.Path) | ||
if match == nil || len(match) < 2 { | ||
fmt.Fprint(w, "error\n") | ||
return | ||
} | ||
client := http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { | ||
return net.Dial("unix", socketPath) | ||
}, | ||
}, | ||
} | ||
|
||
// Convert to number to avoid possible parameter injection | ||
identity, err := strconv.Atoi(match[1]) | ||
if err != nil { | ||
fmt.Fprint(w, "error\n") | ||
return | ||
} | ||
|
||
url := fmt.Sprintf("http://localhost/v1/identity/%d", identity) | ||
resp, err := client.Get(url) | ||
if err != nil { | ||
fmt.Fprint(w, "error\n") | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
io.Copy(w, resp.Body) | ||
} | ||
|
||
func handlePolicy(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, "error\n") | ||
} | ||
|
||
func subMain() error { | ||
pattern, err := regexp.Compile(`^/v1/endpoint/(?P<endpoint>\d+)$`) | ||
endpointPattern = pattern | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pattern, err = regexp.Compile(`^/v1/identity/(?P<identity>\d+)$`) | ||
identityPattern = pattern | ||
if err != nil { | ||
return err | ||
} | ||
|
||
server := http.Server{ | ||
Addr: ":8080", | ||
Handler: nil, | ||
} | ||
|
||
http.HandleFunc("/v1/endpoint/", handleEndpoint) | ||
http.HandleFunc("/v1/identity/", handleIdentity) | ||
http.HandleFunc("/policy/", handlePolicy) | ||
|
||
return server.ListenAndServe() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters