4 Commits

3 changed files with 18 additions and 9 deletions

View File

@@ -5,7 +5,7 @@ NAME = apiary
INSTALL_PREFIX ?= /usr/local INSTALL_PREFIX ?= /usr/local
VERSION = $(shell cat version.go |grep "Version"| cut -d "=" -f2| tr -d "\" ") VERSION = $(shell cat version.go |grep "var Version"| cut -d "=" -f2| tr -d "\" ")
ARCH = $(shell go env | grep GOHOSTARCH | cut -d"=" -f2 | tr -d "\"") ARCH = $(shell go env | grep GOHOSTARCH | cut -d"=" -f2 | tr -d "\"")
OS = $(shell go env | grep GOHOSTOS | cut -d"=" -f2 | tr -d "\"") OS = $(shell go env | grep GOHOSTOS | cut -d"=" -f2 | tr -d "\"")
GIT_COMMIT := $(shell git rev-parse --short HEAD) GIT_COMMIT := $(shell git rev-parse --short HEAD)

View File

@@ -3,6 +3,7 @@ package store
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"net"
_ "github.com/jackc/pgx/v4/stdlib" _ "github.com/jackc/pgx/v4/stdlib"
"github.uio.no/torjus/apiary/models" "github.uio.no/torjus/apiary/models"
@@ -161,6 +162,7 @@ func (s *PostgresStore) statsTotal(limit int) ([]StatsResult, error) {
func (s *PostgresStore) Query(query AttemptQuery) ([]models.LoginAttempt, error) { func (s *PostgresStore) Query(query AttemptQuery) ([]models.LoginAttempt, error) {
var stmt string var stmt string
queryString := query.Query
switch query.QueryType { switch query.QueryType {
case AttemptQueryTypeIP: case AttemptQueryTypeIP:
@@ -168,15 +170,17 @@ func (s *PostgresStore) Query(query AttemptQuery) ([]models.LoginAttempt, error)
FROM login_attempts WHERE remote_ip = $1` FROM login_attempts WHERE remote_ip = $1`
case AttemptQueryTypePassword: case AttemptQueryTypePassword:
stmt = `SELECT id, date, remote_ip, username, password, client_version, connection_uuid, country stmt = `SELECT id, date, remote_ip, username, password, client_version, connection_uuid, country
FROM login_attempts WHERE password like '%$1%'` FROM login_attempts WHERE password like $1`
queryString = fmt.Sprintf("%%%s%%", queryString)
case AttemptQueryTypeUsername: case AttemptQueryTypeUsername:
stmt = `SELECT id, date, remote_ip, username, password, client_version, connection_uuid, country stmt = `SELECT id, date, remote_ip, username, password, client_version, connection_uuid, country
FROM login_attempts WHERE username like '%$1%'` FROM login_attempts WHERE username like $1`
queryString = fmt.Sprintf("%%%s%%", queryString)
default: default:
return nil, fmt.Errorf("Invalid query type") return nil, fmt.Errorf("Invalid query type")
} }
rows, err := s.db.Query(stmt, query.Query) rows, err := s.db.Query(stmt, queryString)
if err != nil { if err != nil {
return nil, fmt.Errorf("Unable to query database: %w", err) return nil, fmt.Errorf("Unable to query database: %w", err)
} }
@@ -185,9 +189,11 @@ func (s *PostgresStore) Query(query AttemptQuery) ([]models.LoginAttempt, error)
var results []models.LoginAttempt var results []models.LoginAttempt
for rows.Next() { for rows.Next() {
var la models.LoginAttempt var la models.LoginAttempt
if err := rows.Scan(&la.ID, &la.Date, &la.RemoteIP, &la.Username, &la.Password, &la.SSHClientVersion, &la.ConnectionUUID, &la.Country); err != nil { var ipString string
if err := rows.Scan(&la.ID, &la.Date, &ipString, &la.Username, &la.Password, &la.SSHClientVersion, &la.ConnectionUUID, &la.Country); err != nil {
return nil, fmt.Errorf("Unable to unmarshal data from database: %w", err) return nil, fmt.Errorf("Unable to unmarshal data from database: %w", err)
} }
la.RemoteIP = net.ParseIP(ipString)
results = append(results, la) results = append(results, la)
} }

View File

@@ -1,14 +1,17 @@
package apiary package apiary
import "fmt" import (
"fmt"
"runtime"
)
var Version = "v0.1.2" var Version = "v0.1.3"
var Build string var Build string
func FullVersion() string { func FullVersion() string {
if Build != "" { if Build != "" {
return fmt.Sprintf("%s-%s", Version, Build) return fmt.Sprintf("%s-%s (%s)", Version, Build, runtime.Version())
} }
return Version return fmt.Sprintf("%s (%s)", Version, runtime.Version())
} }