Skip to content

Commit

Permalink
observability
Browse files Browse the repository at this point in the history
  • Loading branch information
dezren39 committed Jan 10, 2024
1 parent ccd91ac commit 18b1194
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 47 deletions.
2 changes: 2 additions & 0 deletions COPYRIGHT.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Except as otherwise noted below and/or in [`README.md`](README.md) and/or in [`N

A partial list of copyright acknowledgements related to this project.

- within [ src/identity ] (src/identity)
- Charmbracelet, Inc for a wide variety of works - go to [ charm.sh ] (https://charm.sh) [ github.com/charmbracelet ] (https://github.com/charmbracelet)
- within [`build/`](build)
- for nix-related works within [`build/nix`](build/nix) and [`build/buck/preludes/nix-prelude`](build/buck/preludes/nix-prelude)
- SPDX-License-Identifier: MIT OR Apache-2.0
Expand Down
14 changes: 14 additions & 0 deletions sources/identity/auth/observability.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package auth

import (
"github.com/charmbracelet/log"
"github.com/charmbracelet/ssh"
)

func Middleware(h ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
log.Info("Session started", "session", s, "sessionID", s.Context().SessionID(), "user", s.Context().User(), "remoteAddr", s.Context().RemoteAddr().String(), "remoteAddrNetwork", s.Context().RemoteAddr().Network(), "localAddr", s.Context().LocalAddr().String(), "localAddrNetwork", s.Context().LocalAddr().Network(), "charm-id", s.Context().Permissions().Extensions["charm-id"], "charm-name", s.Context().Permissions().Extensions["charm-name"], "charm-roles", s.Context().Permissions().Extensions["charm-roles"], "charm-created-at", s.Context().Permissions().Extensions["charm-created-at"], "charm-public-key-created-at", s.Context().Permissions().Extensions["charm-public-key-created-at"], "charm-public-key-type", s.Context().Permissions().Extensions["charm-public-key-type"], "charm-public-key", s.Context().Permissions().Extensions["charm-public-key"])
h(s)
log.Info("Session ended", "session", s, "sessionID", s.Context().SessionID(), "user", s.Context().User(), "remoteAddr", s.Context().RemoteAddr().String(), "remoteAddrNetwork", s.Context().RemoteAddr().Network(), "localAddr", s.Context().LocalAddr().String(), "localAddrNetwork", s.Context().LocalAddr().Network(), "charm-id", s.Context().Permissions().Extensions["charm-id"], "charm-name", s.Context().Permissions().Extensions["charm-name"], "charm-roles", s.Context().Permissions().Extensions["charm-roles"], "charm-created-at", s.Context().Permissions().Extensions["charm-created-at"], "charm-public-key-created-at", s.Context().Permissions().Extensions["charm-public-key-created-at"], "charm-public-key-type", s.Context().Permissions().Extensions["charm-public-key-type"], "charm-public-key", s.Context().Permissions().Extensions["charm-public-key"])
}
}
177 changes: 130 additions & 47 deletions sources/identity/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,31 @@ import (
"github.com/charmbracelet/wish/logging"
"github.com/charmbracelet/wish/scp"
"github.com/developing-today/code/src/identity/auth"
"github.com/developing-today/code/src/identity/observability"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/kdl"
"github.com/knadh/koanf/providers/file"
"github.com/muesli/reflow/wordwrap"
"github.com/muesli/reflow/wrap"
"github.com/spf13/cobra"
gossh "golang.org/x/crypto/ssh"
)

type errMsg error

type model struct {
spinner spinner.Model
quitting bool
err error
term string
width int
height int
spinner spinner.Model
quitting bool
err error
term string
width int
height int
meltedPrivateKeySeed string
choices []string
cursor int
selected map[int]struct{}
charmId string
publicKeyAuthorized string
}

var quitKeys = key.NewBinding(
Expand All @@ -66,6 +75,29 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit

}
switch msg.String() {
// The "up" and "k" keys move the cursor up
case "up", "k":
if m.cursor > 0 {
m.cursor--
}

// The "down" and "j" keys move the cursor down
case "down", "j":
if m.cursor < len(m.choices)-1 {
m.cursor++
}

// The "enter" key and the spacebar (a literal space) toggle
// the selected state for the item that the cursor is pointing at.
case "enter", " ":
_, ok := m.selected[m.cursor]
if ok {
delete(m.selected, m.cursor)
} else {
m.selected[m.cursor] = struct{}{}
}
}
return m, nil
case tea.WindowSizeMsg:
m.height = msg.Height
Expand All @@ -86,15 +118,64 @@ func (m model) View() string {
s := "Your term is %s\n"
s += "Your window size is x: %d y: %d\n\n"

s = fmt.Sprintf(s, m.term, m.width, m.height)

s += "Which room?\n\n"

for i, choice := range m.choices {

// Is the cursor pointing at this choice?
cursor := " " // no cursor
if m.cursor == i {
cursor = ">" // cursor!
}

// Is this choice selected?
checked := " " // not selected
if _, ok := m.selected[i]; ok {
checked = "x" // selected!
}

s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
}
s += "\n"

if m.meltedPrivateKeySeed != "" {
smelted := "Your private key seed is melted:\n\n%s\n\n"
s += fmt.Sprintf(smelted, m.meltedPrivateKeySeed)
} else {
authorizedPublicKeyText := "Your authorized public key is:\n\n%s\n\n"
s += fmt.Sprintf(authorizedPublicKeyText, m.publicKeyAuthorized)
}
charmIdText := "Your charm id is:\n\n%s\n\n"
s += fmt.Sprintf(charmIdText, m.charmId)

if m.err != nil {
return m.err.Error()
}
str := fmt.Sprintf(s, m.term, m.width, m.height)
str += fmt.Sprintf("\n\n %s Loading forever... %s\n\n", m.spinner.View(), quitKeys.Help().Desc)

s += fmt.Sprintf("\n %s Loading forever... %s\n\n", m.spinner.View(), quitKeys.Help().Desc)

var wrapAt int
if m.width < 24 {
wrapAt = m.width
s = wrap.String(s, wrapAt)
} else {
maxCutoff := 50

// Calculate proportionate cutoff
// Adjust the formula as per your proportionate cutoff logic
wrapAt = m.width - (m.width % 2) // Example formula
if wrapAt > maxCutoff {
wrapAt = maxCutoff
}

s = wordwrap.WrapString(s, wrapAt)
}
if m.quitting {
return str + "\n"
return s + "\n"
}
return str
return s
}

var separator = "."
Expand Down Expand Up @@ -164,17 +245,48 @@ func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
sp := spinner.New()
sp.Spinner = spinner.Dot
sp.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
meltedPrivateKeySeed := s.Context().Permissions().Extensions["private-key-seed-melted"]
m := model{
spinner: sp,
quitting: false,
err: nil,
term: pty.Term,
width: pty.Window.Width,
height: pty.Window.Height,
spinner: sp,
quitting: false,
err: nil,
term: pty.Term,
width: pty.Window.Width,
height: pty.Window.Height,
meltedPrivateKeySeed: meltedPrivateKeySeed,
choices: []string{"Chat", "Game", "Upload"},
selected: make(map[int]struct{}),
charmId: s.Context().Permissions().Extensions["charm-id"],
publicKeyAuthorized: s.Context().Permissions().Extensions["public-key-authorized"],
}
return m, []tea.ProgramOption{tea.WithAltScreen()}
}

func Banner(ctx ssh.Context) string {
return `
Welcome to the identity server!
By using this service, you agree to the following terms and conditions:
- EACH PARTY MAKES NO WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, COMPLETENESS OR PERFORMANCE.
- THE SERVICE AND ANY RELATED SERVICES ARE PROVIDED ON AN "AS IS" AND "AS AVAILABLE" BASIS, WITHOUT WARRANTY OF ANY KIND, WHETHER WRITTEN OR ORAL, EXPRESS OR IMPLIED.
- TO THE FULL EXTENT PERMISSIBLE BY LAW, DEVELOPING.TODAY LLC WILL NOT BE LIABLE FOR ANY DAMAGES OF ANY KIND ARISING FROM THE USE OF ANY DEVELOPING.TODAY LLC SERVICE, OR FROM ANY INFORMATION, CONTENT, MATERIALS, PRODUCTS (INCLUDING SOFTWARE) OR OTHER SERVICES INCLUDED ON OR OTHERWISE MADE AVAILABLE TO YOU THROUGH ANY DEVELOPING.TODAY LLC SERVICE, INCLUDING, BUT NOT LIMITED TO DIRECT, INDIRECT, INCIDENTAL, PUNITIVE, AND CONSEQUENTIAL DAMAGES, UNLESS OTHERWISE SPECIFIED IN WRITING.
If you do not agree to these terms and conditions, you may not use this service and must disconnect immediately.
` + fmt.Sprintf("You are using the identity server at %s:%d\n", configuration.String("host"), configuration.Int("port")) + `
` + fmt.Sprintf("You are connecting from %s\n", ctx.RemoteAddr().String()) + `
` + fmt.Sprintf("You are connecting from-with %s\n", ctx.RemoteAddr().Network()) + `
` + fmt.Sprintf("You are connecting to %s\n", ctx.LocalAddr().String()) + `
` + fmt.Sprintf("You are connecting to-with %s\n", ctx.LocalAddr().Network()) + `
` + fmt.Sprintf("Your server version is %s\n", ctx.ServerVersion()) + `
` + fmt.Sprintf("Your client version is %s\n", ctx.ClientVersion()) + `
` + fmt.Sprintf("Your session id is %s\n", ctx.SessionID()) + `
` + fmt.Sprintf("You are connecting with user %s\n", ctx.User())
}

func start(cmd *cobra.Command, args []string) {
handler := scp.NewFileSystemHandler("./files")
s, err := wish.NewServer(
Expand All @@ -185,13 +297,7 @@ func start(cmd *cobra.Command, args []string) {
elapsed.Middleware(),
promwish.Middleware("0.0.0.0:9222", "identity"),
logging.Middleware(),
func(h ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
log.Info("Session started", "session", s, "sessionID", s.Context().SessionID(), "user", s.Context().User(), "remoteAddr", s.Context().RemoteAddr().String(), "remoteAddrNetwork", s.Context().RemoteAddr().Network(), "localAddr", s.Context().LocalAddr().String(), "localAddrNetwork", s.Context().LocalAddr().Network(), "charm-id", s.Context().Permissions().Extensions["charm-id"], "charm-name", s.Context().Permissions().Extensions["charm-name"], "charm-roles", s.Context().Permissions().Extensions["charm-roles"], "charm-created-at", s.Context().Permissions().Extensions["charm-created-at"], "charm-public-key-created-at", s.Context().Permissions().Extensions["charm-public-key-created-at"], "charm-public-key-type", s.Context().Permissions().Extensions["charm-public-key-type"], "charm-public-key", s.Context().Permissions().Extensions["charm-public-key"])
h(s)
log.Info("Session ended", "session", s, "sessionID", s.Context().SessionID(), "user", s.Context().User(), "remoteAddr", s.Context().RemoteAddr().String(), "remoteAddrNetwork", s.Context().RemoteAddr().Network(), "localAddr", s.Context().LocalAddr().String(), "localAddrNetwork", s.Context().LocalAddr().Network(), "charm-id", s.Context().Permissions().Extensions["charm-id"], "charm-name", s.Context().Permissions().Extensions["charm-name"], "charm-roles", s.Context().Permissions().Extensions["charm-roles"], "charm-created-at", s.Context().Permissions().Extensions["charm-created-at"], "charm-public-key-created-at", s.Context().Permissions().Extensions["charm-public-key-created-at"], "charm-public-key-type", s.Context().Permissions().Extensions["charm-public-key-type"], "charm-public-key", s.Context().Permissions().Extensions["charm-public-key"])
}
},
observability.Middleware(),
),
wish.WithPasswordAuth(func(ctx ssh.Context, password string) bool {
log.Info("Accepting password", "password", password, "len", len(password))
Expand All @@ -205,30 +311,7 @@ func start(cmd *cobra.Command, args []string) {
log.Info("Accepting public key", "publicKeyType", key.Type(), "publicKeyString", base64.StdEncoding.EncodeToString(key.Marshal()))
return Connect(ctx, key, nil, nil)
}),
wish.WithBannerHandler(func(ctx ssh.Context) string {
return `
Welcome to the identity server!
By using this service, you agree to the following terms and conditions:
- EACH PARTY MAKES NO WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, COMPLETENESS OR PERFORMANCE.
- THE SERVICE AND ANY RELATED SERVICES ARE PROVIDED ON AN "AS IS" AND "AS AVAILABLE" BASIS, WITHOUT WARRANTY OF ANY KIND, WHETHER WRITTEN OR ORAL, EXPRESS OR IMPLIED.
- TO THE FULL EXTENT PERMISSIBLE BY LAW, DEVELOPING.TODAY LLC WILL NOT BE LIABLE FOR ANY DAMAGES OF ANY KIND ARISING FROM THE USE OF ANY DEVELOPING.TODAY LLC SERVICE, OR FROM ANY INFORMATION, CONTENT, MATERIALS, PRODUCTS (INCLUDING SOFTWARE) OR OTHER SERVICES INCLUDED ON OR OTHERWISE MADE AVAILABLE TO YOU THROUGH ANY DEVELOPING.TODAY LLC SERVICE, INCLUDING, BUT NOT LIMITED TO DIRECT, INDIRECT, INCIDENTAL, PUNITIVE, AND CONSEQUENTIAL DAMAGES, UNLESS OTHERWISE SPECIFIED IN WRITING.
If you do not agree to these terms and conditions, you may not use this service and must disconnect immediately.
` + fmt.Sprintf("You are using the identity server at %s:%d\n", configuration.String("host"), configuration.Int("port")) + `
` + fmt.Sprintf("You are connecting from %s\n", ctx.RemoteAddr().String()) + `
` + fmt.Sprintf("You are connecting from-with %s\n", ctx.RemoteAddr().Network()) + `
` + fmt.Sprintf("You are connecting to %s\n", ctx.LocalAddr().String()) + `
` + fmt.Sprintf("You are connecting to-with %s\n", ctx.LocalAddr().Network()) + `
` + fmt.Sprintf("Your server version is %s\n", ctx.ServerVersion()) + `
` + fmt.Sprintf("Your client version is %s\n", ctx.ClientVersion()) + `
` + fmt.Sprintf("Your session id is %s\n", ctx.SessionID()) + `
` + fmt.Sprintf("You are connecting with user %s\n", ctx.User())
}),
wish.WithBannerHandler(Banner),
wish.WithAddress(fmt.Sprintf("%s:%d", configuration.String("host"), configuration.Int("port"))),
wish.WithHostKeyPath(hostKeyPath),
)
Expand Down

0 comments on commit 18b1194

Please sign in to comment.