Public release of katbox

This commit is contained in:
Renán Del Valle 2021-04-12 12:00:01 -07:00
commit 4c764e09a4
46 changed files with 4646 additions and 0 deletions

23
stream/Dockerfile Normal file
View file

@ -0,0 +1,23 @@
#Build from golang alpine as base image and add build base
FROM golang:1.16.0-alpine3.13 as golangbase
RUN apk add build-base
LABEL maintainer="The Katbox Authors"
WORKDIR /app
# Copy the source from the current directory to the Working Directory inside the container
COPY . .
RUN go mod download
RUN go build -o main .
#Start a new stage from scratch (multistage build)
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
# Copy the Pre-built binary file from the previous stage
COPY --from=golangbase /app/main .
#TODO:@revanth this port needs to updated before deploying on the cluster if needed
EXPOSE 5051
CMD ["./main"]

232
stream/docs/docs.go Normal file
View file

@ -0,0 +1,232 @@
// swag init
// Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag
package docs
import (
"bytes"
"encoding/json"
"strings"
"text/template"
"github.com/swaggo/swag"
)
var doc = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{.Description}}",
"title": "{{.Title}}",
"contact": {
"name": "Revanth Chandra",
},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/files/browse": {
"get": {
"description": "serves sandbox logs filesystem as a json object",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Browse Filesystem",
"parameters": [
{
"type": "string",
"description": "Path",
"name": "path",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/main.Result"
}
}
}
}
},
"/files/download": {
"get": {
"description": "Download any file from sandbox logs filesystem",
"consumes": [
"application/json"
],
"produces": [
"application/octet-stream"
],
"summary": "Download a file from Filesystem",
"parameters": [
{
"type": "string",
"description": "Path",
"name": "path",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/main.StreamData"
}
}
}
}
},
"/files/read": {
"get": {
"description": "Reads any file from sandbox logs filesystem and serves as a json object",
"consumes": [
"application/json"
],
"summary": "Read a file from Filesystem",
"parameters": [
{
"type": "string",
"description": "Path",
"name": "path",
"in": "query",
"required": true
},
{
"type": "integer",
"description": "Offset",
"name": "offset",
"in": "query",
"required": true
},
{
"type": "integer",
"description": "Length",
"name": "length",
"in": "query",
"required": true
},
{
"type": "string",
"description": "jsonp",
"name": "jsonp",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/main.StreamData"
}
}
}
}
}
},
"definitions": {
"main.FileInformation": {
"type": "object",
"properties": {
"gid": {
"type": "string"
},
"mode": {
"type": "string"
},
"mtime": {
"type": "string"
},
"nlink": {
"type": "string"
},
"path": {
"type": "string"
},
"size": {
"type": "string"
},
"uid": {
"type": "string"
}
}
},
"main.Result": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/main.FileInformation"
}
}
}
},
"main.StreamData": {
"type": "object",
"properties": {
"data": {
"type": "string"
},
"offset": {
"type": "integer"
}
}
}
}
}`
type swaggerInfo struct {
Version string
Host string
BasePath string
Schemes []string
Title string
Description string
}
// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = swaggerInfo{
Version: "1.0",
Host: "localhost:8080",
BasePath: "/",
Schemes: []string{},
Title: "k8s Sandbox Go Restful API with Swagger",
Description: "Rest API doc for sandbox API's",
}
type s struct{}
func (s *s) ReadDoc() string {
sInfo := SwaggerInfo
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)
t, err := template.New("swagger_info").Funcs(template.FuncMap{
"marshal": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
}).Parse(doc)
if err != nil {
return doc
}
var tpl bytes.Buffer
if err := t.Execute(&tpl, sInfo); err != nil {
return doc
}
return tpl.String()
}
func init() {
swag.Register(swag.Name, &s{})
}

170
stream/docs/swagger.json Normal file
View file

@ -0,0 +1,170 @@
{
"swagger": "2.0",
"info": {
"description": "Rest API doc for sandbox API's",
"title": "k8s Sandbox Go Restful API with Swagger",
"contact": {
"name": "Revanth Chandra",
},
"version": "1.0"
},
"host": "localhost:8080",
"basePath": "/",
"paths": {
"/files/browse": {
"get": {
"description": "serves sandbox logs filesystem as a json object",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Browse Filesystem",
"parameters": [
{
"type": "string",
"description": "Path",
"name": "path",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/main.Result"
}
}
}
}
},
"/files/download": {
"get": {
"description": "Download any file from sandbox logs filesystem",
"consumes": [
"application/json"
],
"produces": [
"application/octet-stream"
],
"summary": "Download a file from Filesystem",
"parameters": [
{
"type": "string",
"description": "Path",
"name": "path",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/main.StreamData"
}
}
}
}
},
"/files/read": {
"get": {
"description": "Reads any file from sandbox logs filesystem and serves as a json object",
"consumes": [
"application/json"
],
"summary": "Read a file from Filesystem",
"parameters": [
{
"type": "string",
"description": "Path",
"name": "path",
"in": "query",
"required": true
},
{
"type": "integer",
"description": "Offset",
"name": "offset",
"in": "query",
"required": true
},
{
"type": "integer",
"description": "Length",
"name": "length",
"in": "query",
"required": true
},
{
"type": "string",
"description": "jsonp",
"name": "jsonp",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/main.StreamData"
}
}
}
}
}
},
"definitions": {
"main.FileInformation": {
"type": "object",
"properties": {
"gid": {
"type": "string"
},
"mode": {
"type": "string"
},
"mtime": {
"type": "string"
},
"nlink": {
"type": "string"
},
"path": {
"type": "string"
},
"size": {
"type": "string"
},
"uid": {
"type": "string"
}
}
},
"main.Result": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/main.FileInformation"
}
}
}
},
"main.StreamData": {
"type": "object",
"properties": {
"data": {
"type": "string"
},
"offset": {
"type": "integer"
}
}
}
}
}

113
stream/docs/swagger.yaml Normal file
View file

@ -0,0 +1,113 @@
basePath: /
definitions:
main.FileInformation:
properties:
gid:
type: string
mode:
type: string
mtime:
type: string
nlink:
type: string
path:
type: string
size:
type: string
uid:
type: string
type: object
main.Result:
properties:
data:
items:
$ref: '#/definitions/main.FileInformation'
type: array
type: object
main.StreamData:
properties:
data:
type: string
offset:
type: integer
type: object
host: localhost:8080
info:
contact:
name: Revanth Chandra
description: Rest API doc for sandbox API's
title: k8s Sandbox Go Restful API with Swagger
version: "1.0"
paths:
/files/browse:
get:
consumes:
- application/json
description: serves sandbox logs filesystem as a json object
parameters:
- description: Path
in: query
name: path
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/main.Result'
summary: Browse Filesystem
/files/download:
get:
consumes:
- application/json
description: Download any file from sandbox logs filesystem
parameters:
- description: Path
in: query
name: path
required: true
type: string
produces:
- application/octet-stream
responses:
"200":
description: OK
schema:
$ref: '#/definitions/main.StreamData'
summary: Download a file from Filesystem
/files/read:
get:
consumes:
- application/json
description: Reads any file from sandbox logs filesystem and serves as a json
object
parameters:
- description: Path
in: query
name: path
required: true
type: string
- description: Offset
in: query
name: offset
required: true
type: integer
- description: Length
in: query
name: length
required: true
type: integer
- description: jsonp
in: query
name: jsonp
required: true
type: string
responses:
"200":
description: OK
schema:
$ref: '#/definitions/main.StreamData'
summary: Read a file from Filesystem
swagger: "2.0"

9
stream/go.mod Normal file
View file

@ -0,0 +1,9 @@
module stream
go 1.16
require (
github.com/swaggo/http-swagger v1.0.0 // indirect
github.com/swaggo/swag v1.7.0 // indirect
golang.org/x/tools v0.1.0 // indirect
)

97
stream/go.sum Normal file
View file

@ -0,0 +1,97 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonreference v0.19.4/go.mod h1:RdybgQwPxbL4UEjuAruzK1x3nE69AqPYEJeo/TWfEeg=
github.com/go-openapi/jsonreference v0.19.5 h1:1WJP/wi4OjB4iV8KVbH73rQaoialJrqv8gitZLxGLtM=
github.com/go-openapi/jsonreference v0.19.5/go.mod h1:RdybgQwPxbL4UEjuAruzK1x3nE69AqPYEJeo/TWfEeg=
github.com/go-openapi/spec v0.19.14/go.mod h1:gwrgJS15eCUgjLpMjBJmbZezCsw88LmgeEip0M63doA=
github.com/go-openapi/spec v0.20.0 h1:HGLc8AJ7ynOxwv0Lq4TsnwLsWMawHAYiJIFzbcML86I=
github.com/go-openapi/spec v0.20.0/go.mod h1:+81FIL1JwC5P3/Iuuozq3pPE9dXdIEGxFutcFKaVbmU=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.11/go.mod h1:Uc0gKkdR+ojzsEpjh39QChyu92vPgIr72POcgHMAgSY=
github.com/go-openapi/swag v0.19.12 h1:Bc0bnY2c3AoF7Gc+IMIAQQsD8fLHjHpc19wXvYuayQI=
github.com/go-openapi/swag v0.19.12/go.mod h1:eFdyEBkTdoAf/9RXBvj4cr1nH7GD8Kzo5HTt47gr72M=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA=
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14 h1:PyYN9JH5jY9j6av01SpfRMb+1DWg/i3MbGOKPxJ2wjM=
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14/go.mod h1:gxQT6pBGRuIGunNf/+tSOB5OHvguWi8Tbt82WOkf35E=
github.com/swaggo/http-swagger v1.0.0 h1:ksYgVBCYmAaxFsGVGojlPROgYfiQQSllETTWMtHJHTo=
github.com/swaggo/http-swagger v1.0.0/go.mod h1:cKIcshBU9yEAnfWv6ZzVKSsEf8h5ozxB8/zHQWyOQ/8=
github.com/swaggo/swag v1.7.0 h1:5bCA/MTLQoIqDXXyHfOpMeDvL9j68OY/udlK4pQoo4E=
github.com/swaggo/swag v1.7.0/go.mod h1:BdPIL73gvS9NBsdi7M1JOxLvlbfvNRaBP8m6WT6Aajo=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201207224615-747e23833adb h1:xj2oMIbduz83x7tzglytWT7spn6rP+9hvKjTpro6/pM=
golang.org/x/net v0.0.0-20201207224615-747e23833adb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201120155355-20be4ac4bd6e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20201208062317-e652b2f42cc7 h1:2OSu5vYyX4LVqZAtqZXnFEcN26SDKIJYlEVIRl1tj8U=
golang.org/x/tools v0.0.0-20201208062317-e652b2f42cc7/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

23
stream/k8s.yaml Normal file
View file

@ -0,0 +1,23 @@
#---#
#k8s-base.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: streamserver
spec:
replicas: 1
selector:
matchLabels:
app: streamserver
template:
metadata:
labels:
app: streamserver
spec:
containers:
- name: streamserver
image: streamserver:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080

347
stream/main.go Normal file
View file

@ -0,0 +1,347 @@
package main
/*
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
*/
import "C"
import (
"encoding/json"
"fmt"
"io"
"log"
"math"
"net/http"
"os"
"os/user"
"path/filepath"
"strconv"
_ "stream/docs"
"strings"
"syscall"
httpSwagger "github.com/swaggo/http-swagger"
)
type StreamData struct {
Data string `json:"data"`
Offset int64 `json:"offset"`
}
type FileInformation struct {
Mode string `json:"mode"`
Nlink string `json:"nlink"`
UID string `json:"uid"`
GID string `json:"gid"`
Size string `json:"size"`
Mtime int64 `json:"mtime"`
Path string `json:"path"`
}
type Result struct {
Data []FileInformation `json:"data`
}
type Error struct {
Error string `json:"error"`
}
// Download a file from sandbox filesystem with given offset and length godoc
// @Summary Download a file from Filesystem
// @Description Download any file from sandbox logs filesystem
// @Accept json
// @Produce octet-stream
// @Success 200 {object} main.StreamData
// @Router /files/download [get]
// @Param path query string true "Path"
func downloadhandler(w http.ResponseWriter, r *http.Request) {
//Get query params
params := r.URL.Query()
path := params["path"]
if len(path) == 0 {
log.Println("path not found in URL")
error := &Error{
Error: "path not found in URL"}
retObj, err := json.Marshal(error)
if err != nil {
log.Print(err)
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(retObj)
return
}
file, err := os.Open(path[0])
if err != nil {
log.Print(err)
}
filename := filepath.Base(path[0])
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
io.Copy(w, file)
file.Close()
}
// read a file from sandbox filesystem with given offset and length godoc
// @Summary Read a file from Filesystem
// @Description Reads any file from sandbox logs filesystem and serves as a json object
// @Accept json
// @Success 200 {object} main.StreamData
// @Router /files/read [get]
// @Param path query string true "Path"
// @Param offset query int true "Offset"
// @Param length query int true "Length"
// @Param jsonp query string true "jsonp"
func readHandler(w http.ResponseWriter, r *http.Request) {
// Get query path params
params := r.URL.Query()
path := params["path"]
//check if path exists in the query params if not send error response
if len(path) == 0 {
log.Println("path not found in URL")
error := &Error{
Error: "path not found in URL"}
retObj, err := json.Marshal(error)
if err != nil {
log.Print(err)
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(retObj)
return
}
var err error
//check if offset exists in the query params if not send error response
if len(params["offset"]) == 0 {
error := &Error{
Error: "offset not found in URL"}
retObj, err := json.Marshal(error)
if err != nil {
log.Print(err)
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(retObj)
return
}
offset, err := strconv.ParseInt(params["offset"][0], 10, 64)
if err != nil {
log.Print(err)
}
//check if length exists in the query params if not send error response
if len(params["length"]) == 0 {
error := &Error{
Error: "length not found in URL"}
retObj, err := json.Marshal(error)
if err != nil {
log.Print(err)
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(retObj)
return
}
length, err := strconv.ParseInt(params["length"][0], 10, 64)
if err != nil {
log.Print(err)
}
size := getSize(path[0])
// if offset is invalid set it to size of the file
if offset == -1 {
offset = size
}
//set length if it is invalid
if length == -1 {
length = size - offset
}
// Cap the read length at 16 pages.
length = int64(math.Min(float64(length), float64(C.sysconf(C._SC_PAGE_SIZE)*16)))
// return empty data when offset is greater than size of the file
if offset >= size {
staremData := &StreamData{
Data: "",
Offset: size}
respObj, err := json.Marshal(staremData)
if err != nil {
fmt.Fprintf(w, "json encode error")
return
}
callbackName := r.URL.Query().Get("jsonp")
if callbackName == "" {
fmt.Fprintf(w, "Please give callback name in query string")
return
}
w.Header().Set("Content-Type", "application/javascript")
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprintf(w, "%s(%s);", callbackName, respObj)
return
}
file, err := os.Open(path[0])
if err != nil {
log.Print(err)
}
defer file.Close()
s := io.NewSectionReader(file, 0, size)
buf2 := make([]byte, length)
_, err = s.ReadAt(buf2, offset)
if err != nil {
log.Print(err)
}
staremData := &StreamData{
Data: string(buf2),
Offset: offset}
respObj, err := json.Marshal(staremData)
if err != nil {
fmt.Fprintf(w, "json encode error")
return
}
callbackName := r.URL.Query().Get("jsonp")
if callbackName == "" {
fmt.Fprintf(w, "Please give callback name in query string")
return
}
w.Header().Set("Content-Type", "application/javascript")
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprintf(w, "%s(%s);", callbackName, respObj)
}
// browse sandbox filesystem godoc
// @Summary Browse Filesystem
// @Description serves sandbox logs filesystem as a json object
// @Accept json
// @Produce json
// @Success 200 {object} main.Result
// @Router /files/browse [get]
// @Param path query string true "Path"
func browseHandler(w http.ResponseWriter, r *http.Request) {
//Get query Params
params := r.URL.Query()
path := params["path"]
//check if path exists in the query params if not send error response
if len(path) == 0 {
log.Println("path not found in URL")
error := &Error{
Error: "path not found in URL"}
retObj, err := json.Marshal(error)
if err != nil {
log.Print(err)
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(retObj)
return
}
// walk through the directory structure and build fileInformation struct and append to result array
result := []FileInformation{}
//test := strings.Split(path[0], "/")[len(strings.Split(path[0], "/"))-1]
//fmt.Print(test, " ")
//fmt.Print(test, " ")
err := filepath.WalkDir(path[0],
func(filePath string, dirEntry os.DirEntry, err error) error {
if len(strings.Split(filePath, "/")) < len(strings.Split(path[0], "/"))+2 {
if err != nil {
return err
}
fileInfo, err := dirEntry.Info()
fileStat, err := os.Stat(filePath)
if err != nil {
return err
}
// retrieve file ownership information and number of hardlinks to the file
var nlink, uid, gid uint64
if sys := fileStat.Sys(); sys != nil {
if stat, ok := sys.(*syscall.Stat_t); ok {
nlink = uint64(stat.Nlink)
uid = uint64(stat.Uid)
gid = uint64(stat.Gid)
}
}
usr, err := user.LookupId(strconv.FormatUint(uid, 10))
group, err := user.LookupGroupId(strconv.FormatUint(gid, 10))
if err != nil {
return err
}
jsonData := FileInformation{
Mode: fileInfo.Mode().String(),
Nlink: string(strconv.FormatUint(nlink, 10)),
UID: usr.Username,
GID: group.Name,
Size: strconv.Itoa(int(fileInfo.Size())),
Mtime: fileInfo.ModTime().Unix(),
Path: filePath,
}
if err == nil {
result = append(result, jsonData)
}
}
return nil
})
if err != nil {
log.Println(err)
}
resultdata := Result{result}
c, err := json.Marshal(resultdata)
if err == nil {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(c)
}
}
//Get the size of a file
func getSize(p string) int64 {
if stat, err := os.Stat(p); err == nil {
return stat.Size()
}
return 0
}
// @title k8s Sandbox Go Restful API with Swagger
// @version 1.0
// @description Rest API doc for sandbox API's
// @contact.name Revanth Chandra
// @host localhost:8080
// @BasePath /
func main() {
http.HandleFunc("/files/read", readHandler)
http.HandleFunc("/files/browse", browseHandler)
http.HandleFunc("/files/download", downloadhandler)
http.HandleFunc("/swagger/", httpSwagger.WrapHandler)
http.ListenAndServe(":5051", nil)
}

127
stream/main_test.go Normal file
View file

@ -0,0 +1,127 @@
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestBrowseHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/files/browse", nil)
if err != nil {
t.Fatal(err)
}
q := req.URL.Query()
q.Add("path", ".")
req.URL.RawQuery = q.Encode()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(browseHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
func TestReadHandlerWIthVakidOffset(t *testing.T) {
req, err := http.NewRequest("GET", "/files/read", nil)
if err != nil {
t.Fatal(err)
}
q := req.URL.Query()
q.Add("path", "/Users/revchandra/Desktop/go/dce.err")
q.Add("offset", "0")
q.Add("length", "1000")
q.Add("jsonp", "jQuery17107124409226948478_1614562818140&_=1614562818159")
req.URL.RawQuery = q.Encode()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(readHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
func TestReadHandlerWithOffsetOverflow(t *testing.T) {
req, err := http.NewRequest("GET", "/files/read", nil)
if err != nil {
t.Fatal(err)
}
q := req.URL.Query()
q.Add("path", ".")
q.Add("offset", "4174")
q.Add("length", "1000")
q.Add("jsonp", "jQuery17107124409226948478_1614562818140&_=1614562818159")
req.URL.RawQuery = q.Encode()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(readHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
func TestReadHandlerWithInvalidOffset(t *testing.T) {
req, err := http.NewRequest("GET", "/files/read", nil)
if err != nil {
t.Fatal(err)
}
q := req.URL.Query()
q.Add("path", ".")
q.Add("offset", "-1")
q.Add("length", "-1")
q.Add("jsonp", "jQuery17107124409226948478_1614562818140&_=1614562818159")
req.URL.RawQuery = q.Encode()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(readHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
func TestDownloadHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/files/download", nil)
if err != nil {
t.Fatal(err)
}
q := req.URL.Query()
q.Add("path", ".")
req.URL.RawQuery = q.Encode()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(downloadhandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
func TestDownloadHandlerWithInvalidPath(t *testing.T) {
req, err := http.NewRequest("GET", "/files/download", nil)
if err != nil {
t.Fatal(err)
}
q := req.URL.Query()
q.Add("path", ".")
req.URL.RawQuery = q.Encode()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(downloadhandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}