/weblog

Web service - Registered sources save log messages to mongodb [WIP]

Primary LanguageGoMIT LicenseMIT

mongodb web app - web interface for logging events [WIP]

In order to allow system with signature ID SYSTEMXYZ to log message System failure detected

Configure signature ID in whitelist.json like that:

{
    "systems": [{
            "ID": "SYSTEMXYZ",
            "Description": "Mail server 1"
        },
        {
            "ID": "SYSTEMABC",
            "Description": "File server 1"
        },
        {
            "ID": "SYSTEMWZT",
            "Description": "File server 2"
        }
    ]
}

Run:

go run main.go

Call /save with parameter signature "SYSTEMXYZ" and message "System failure detected":

https://127.0.0.1/save?signature=SYSTEMXYZ&message=System failure detected

Result:

{ "_id" : ObjectId("6193e1520dcdee3f35f5faff"), "signature" : "SYSTEMXYZ", "ts" : ISODate("2021-11-16T16:50:26.014Z"), "message" : "system failure detected on XYZ" }

If host is not allowed (e.g: SYSTEMFOO, its signature ID is not in whitelist), then result is:

https://127.0.0.1/save?signature=SYSTEMXYZ&message=system failure on XYZ

Result:

Error: authentication denied

In order to retrieve objects for registered systems, you have to call /load with parameter signature:

https://127.0.0.1/save?signature=SYSTEMXYZ

Result:

[
    {
        "Signature": "SYSTEMXYZ",
        "TS": "2021-11-16T16:50:26.014Z",
        "Message": "system failure detected on XYZ"
    },
    {
        "Signature": "SYSTEMXYZ",
        "TS": "2021-11-16T17:37:06.46Z",
        "Message": "system failure detected on XYZ"
    },
    {
        "Signature": "SYSTEMXYZ",
        "TS": "2021-11-16T17:59:31.746Z",
        "Message": "system failure detected on XYZ"
    }
]

Environment setup:

export GOPATH=${HOME}/go
export GOBIN=${GOPATH}/bin
export PATH=${PATH}:${GOBIN}
export PATH=${PATH}:$( dirname $( which go ) )

Run:

sudo --preserve-env=GOPATH,GOBIN,PATH make run

Build:

sudo --preserve-env=GOPATH,GOBIN,PATH make build

Test:

sudo --preserve-env=GOPATH,GOBIN,PATH make test

Test coverage details:

(main)$ go test -v -cover ./...
?   	github.com/deeper-x/weblog	[no test files]
=== RUN   TestNewInstance
--- PASS: TestNewInstance (0.00s)
=== RUN   TestCreateCtx
--- PASS: TestCreateCtx (0.00s)
=== RUN   TestCreateClient
--- PASS: TestCreateClient (0.00s)
=== RUN   TestCreateCollection
--- PASS: TestCreateCollection (0.00s)
=== RUN   TestConnect
--- PASS: TestConnect (0.00s)
=== RUN   TestClose
--- PASS: TestClose (0.00s)
=== RUN   TestAddEntry
--- PASS: TestAddEntry (0.01s)
=== RUN   TestSaveEntry
--- PASS: TestSaveEntry (0.00s)
=== RUN   TestGetEntries
--- PASS: TestGetEntries (0.00s)
PASS
coverage: 59.4% of statements
ok  	github.com/deeper-x/weblog/db	0.038s	coverage: 59.4% of statements
?   	github.com/deeper-x/weblog/messages	[no test files]
?   	github.com/deeper-x/weblog/settings	[no test files]
=== RUN   TestReadJSONFile
--- PASS: TestReadJSONFile (0.00s)
PASS
coverage: 46.2% of statements
ok  	github.com/deeper-x/weblog/wauth	0.004s	coverage: 46.2% of statements
=== RUN   TestSave
2021/11/17 12:30:56 Saving entry - Signature: [SYSTEMXYZ]
2021/11/17 12:30:56 Entry saved succesfully--- PASS: TestSave (0.01s)
=== RUN   TestLoad
--- PASS: TestLoad (0.01s)
PASS
coverage: 40.8% of statements
ok  	github.com/deeper-x/weblog/web	0.029s	coverage: 40.8% of statements

Additionale notes:

TLS deploy:

# key 2048 bit - RSA
openssl genrsa -out tls/server.key 2048
# self-signed cert (x509) pem encoded, no password
openssl req -new -x509 -sha256 -key tls/server.key -out tls/server.crt -days 365