Commit 7f46e527 authored by ale's avatar ale

Merge branch 'freewvs' into 'master'

Add webappdb lookups for website resources

See merge request !8
parents 1833a56c ea4bfd53
Pipeline #5580 passed with stages
in 4 minutes and 41 seconds
package webappdbbackend
import (
"context"
"log"
as "git.autistici.org/ai3/accountserver"
"git.autistici.org/ai3/go-common/clientutil"
"git.autistici.org/ai3/tools/webappdb/proto"
)
// WebappDBBackend looks up website information in the webappdb
// (FreeWVS) service.
type WebappDBBackend struct {
as.Backend
dbbe clientutil.Backend
}
type wdbTX struct {
as.TX
dbbe clientutil.Backend
}
func Wrap(b as.Backend, config *clientutil.BackendConfig) (*WebappDBBackend, error) {
be, err := clientutil.NewBackend(config)
if err != nil {
return nil, err
}
return &WebappDBBackend{
Backend: b,
dbbe: be,
}, nil
}
func (b *WebappDBBackend) NewTransaction() (as.TX, error) {
tx, err := b.Backend.NewTransaction()
if err != nil {
return nil, err
}
return &wdbTX{
TX: tx,
dbbe: b.dbbe,
}, nil
}
func (tx *wdbTX) GetUser(ctx context.Context, name string) (*as.RawUser, error) {
user, err := tx.TX.GetUser(ctx, name)
if err != nil {
return nil, err
}
// Find all web resources.
var sites []string
sitemap := make(map[string]*as.Resource)
for _, r := range user.Resources {
if r.Type == as.ResourceTypeWebsite || r.Type == as.ResourceTypeDomain {
sites = append(sites, r.Name)
sitemap[r.Name] = r
}
}
var cmsinfo map[string][]*webappdb.App
if len(sitemap) == 0 {
goto skip
}
cmsinfo, err = tx.lookup(ctx, sites)
if err != nil {
log.Printf("error looking up webappdb: %v", err)
goto skip
}
for key, values := range cmsinfo {
r, ok := sitemap[key]
if !ok || r.Website == nil {
continue
}
r.Website.CMSInfo = values
}
skip:
return user, nil
}
func (tx *wdbTX) GetResource(ctx context.Context, id as.ResourceID) (*as.RawResource, error) {
rsrc, err := tx.TX.GetResource(ctx, id)
if err != nil {
return nil, err
}
if rsrc.Type != as.ResourceTypeWebsite && rsrc.Type != as.ResourceTypeDomain {
return rsrc, nil
}
if tmp, err := tx.lookup(ctx, []string{rsrc.Name}); err == nil {
rsrc.Website.CMSInfo = tmp[rsrc.Name]
}
return rsrc, nil
}
func (tx *wdbTX) lookup(ctx context.Context, sites []string) (map[string][]*webappdb.App, error) {
var resp webappdb.FindAppsBySiteResponse
if err := tx.dbbe.Call(ctx, "", "/api/search/by_site", &webappdb.FindAppsBySiteRequest{
Sites: sites,
}, &resp); err != nil {
return nil, err
}
return resp.Apps, nil
}
......@@ -16,6 +16,7 @@ import (
cachebackend "git.autistici.org/ai3/accountserver/backend/cache"
ldapbackend "git.autistici.org/ai3/accountserver/backend/ldap"
webappdbbackend "git.autistici.org/ai3/accountserver/backend/webappdb"
"git.autistici.org/ai3/accountserver/server"
)
......@@ -41,6 +42,7 @@ type config struct {
Algo string `yaml:"algo"`
Params map[string]int `yaml:"params"`
} `yaml:"pwhash"`
WebappDB *clientutil.BackendConfig `yaml:"webappdb"`
// Replication config.
Replication struct {
......@@ -184,6 +186,17 @@ func main() {
be = cache
}
// Enable lookups to the webappdb (FreeWVS) service. Errors
// are not fatal, the service is optional.
if config.WebappDB != nil {
wdb, err := webappdbbackend.Wrap(be, config.WebappDB)
if err != nil {
log.Printf("warning: could not initialize webappdb backend: %v", err)
} else {
be = wdb
}
}
service, err := accountserver.NewAccountService(be, &config.AccountServerConfig)
if err != nil {
log.Fatal(err)
......
......@@ -8,6 +8,7 @@ import (
"time"
"git.autistici.org/ai3/go-common/pwhash"
"git.autistici.org/ai3/tools/webappdb/proto"
)
// Possible values for user status.
......@@ -567,8 +568,7 @@ type Website struct {
QuotaUsage int `json:"quota_usage"`
DocumentRoot string `json:"document_root"`
CMSInfo map[string]*CMSInfo `json:"cms_info,omitempty"`
VulnerabilityInfo map[string]*VulnInfo `json:"vulnerability_info,omitempty"`
CMSInfo []*webappdb.App `json:"cms_info,omitempty"`
}
// Database resource attributes.
......
package webappdb
import "time"
// App stores information about an application instance. The JSON
// fields should match the XML fields as used by 'freewvs --xml'.
type App struct {
Shard string `json:"shard"`
Path string `json:"directory"`
Site string `json:"site"`
Name string `json:"appname"`
Version string `json:"version"`
SafeVersion string `json:"safeversion"`
State string `json:"state"`
VulnInfo string `json:"vulninfo"`
Timestamp time.Time `json:"timestamp"`
}
// SubmissionRequest RPC.
type SubmissionRequest struct {
Shard string `json:"shard"`
Entries []*App `json:"entries"`
}
// FindAppsBySiteRequest RPC, handles multiple sites in a single
// request to minimize RPC latency.
type FindAppsBySiteRequest struct {
Sites []string `json:"sites"`
}
// FindAppsBySiteResponse is the response type for FindAppsBySiteRequest.
type FindAppsBySiteResponse struct {
Apps map[string][]*App `json:"apps"`
}
......@@ -50,6 +50,12 @@
"revision": "54f0ac4c46184ae44486a31ca2705076abcc5321",
"revisionTime": "2019-06-30T08:30:15Z"
},
{
"checksumSHA1": "hC2fGBA4OHGjbaBEqW2f+qq9dic=",
"path": "git.autistici.org/ai3/tools/webappdb/proto",
"revision": "42f0d2615a045d72f2a71d2ad328ebabd2f16176",
"revisionTime": "2020-01-04T23:46:21Z"
},
{
"checksumSHA1": "yRc5umgrV1NRzXHqjkHSUAXIvpg=",
"origin": "git.autistici.org/id/usermetadb/vendor/git.autistici.org/id/auth",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment