Skip to content
46 changes: 21 additions & 25 deletions api/log-replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strconv"
"strings"
"time"

"github.com/journeymidnight/yig/helper"
"github.com/journeymidnight/yig/signature"
)
Expand Down Expand Up @@ -284,10 +284,12 @@ func (r *replacer) getSubstitution(key string) string {
// TODO: Add bucket logging
return strconv.FormatBool(false)
case "{cdn_request}":
allPluginMap := mods.InitialPlugins()
InitializeJudgeClient(allPluginMap)
var judgeFunc JudgeCdnRequest
judgeFunc = JudgeCDNRequest
err := InitializeJudgeCdnFunc(mods.Container)
if err != nil {
return "-"
}
var judgeFunc IsCdnRequest
judgeFunc = JudgeCdnRequest
return strconv.FormatBool(judgeFunc(r.request))
default:
return "-"
Expand Down Expand Up @@ -361,34 +363,28 @@ func inRange(r ipRange, ipAddress net.IP) bool {
return false
}

type JudgeCdnRequest func(r *http.Request) bool
type IsCdnRequest func(r *http.Request) bool

type JudgeClient interface {
JudgeCDNRequest(r *http.Request) bool
type JudgeCdnFunc interface {
JudgeCdnRequest(r *http.Request) bool
}

var judgeClient JudgeClient
var judgeCdnFunc JudgeCdnFunc

func InitializeJudgeClient(plugins map[string]*mods.YigPlugin) {
func InitializeJudgeCdnFunc(plugins map[string]*mods.YigPlugin) error {
//Search for JudgeCDN plugins, if we have many JudgeCDN plugins, always use the first
name := "cdn_judge"
p := plugins["cdn_judge"]
if p.PluginType == mods.JUDGE_PLUGIN {
c, err := p.Create(helper.CONFIG.Plugins[name].Args)
if err != nil {
helper.Logger.Fatalf(0, "failed to initial JudgeCDN plugin %s: err: %v\n", name, err)
return
}
helper.Logger.Printf(5, "Chosen JudgeCDN plugin %s..\n", name)
judgeClient = c.(JudgeClient)
return
c, err := p.Create(helper.CONFIG.Plugins[name].Args)
if err != nil {
helper.Logger.Fatalf(0, "failed to initial cdn_Judge plugin %s: err: %v\n", name, err)
return err
}
helper.Logger.Printf(5, "Chosen JudgeCDN plugin %s..\n", name)
judgeCdnFunc = c.(JudgeCdnFunc)
return nil
}

func JudgeCDNRequest(r *http.Request) bool {
cdnFlag, ok := r.URL.Query()["X-Oss-Referer"]
if ok && len(cdnFlag) > 0 && cdnFlag[0] == "cdn" {
return true
}
return false
func JudgeCdnRequest(r *http.Request) bool {
return judgeCdnFunc.JudgeCdnRequest(r)
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func main() {

//Read all *.so from plugins directory, and fill the varaible allPlugins
allPluginMap := mods.InitialPlugins()

iam.InitializeIamClient(allPluginMap)
mods.Container = allPluginMap

startAdminServer(adminServerConfig)

Expand Down
2 changes: 2 additions & 0 deletions mods/yig-plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type YigPlugin struct {
Create func(map[string]interface{}) (interface{}, error)
}

var Container map[string]*YigPlugin

const EXPORTED_PLUGIN = "Exported"

const (
Expand Down
10 changes: 5 additions & 5 deletions plugins/judge_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ const pluginName = "cdn_judge"
var Exported = mods.YigPlugin{
Name: pluginName,
PluginType: mods.JUDGE_PLUGIN,
Create: GetJudgeClient,
Create: GetJudgeCdnFunc,
}


func GetJudgeClient(config map[string]interface{}) (interface{}, error) {
func GetJudgeCdnFunc(config map[string]interface{}) (interface{}, error) {

helper.Logger.Printf(10, "Get plugin config: %v\n", config)

c := JudgeClient{
c := JudgeCdnFunc{
JudgeCdnTarget: config["target"].(string),
}

return interface{}(c), nil
}

type JudgeClient struct {
type JudgeCdnFunc struct {
JudgeCdnTarget string
}

func (j JudgeClient) JudgeCDNRequest(r *http.Request) bool {
func (j JudgeCdnFunc) JudgeCdnRequest(r *http.Request) bool {
cdnFlag, ok := r.URL.Query()[j.JudgeCdnTarget]
if ok && len(cdnFlag) > 0 && cdnFlag[0] == "cdn" {
return true
Expand Down
26 changes: 9 additions & 17 deletions test/go/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,7 @@ import (
const timeLayoutStr = "2006-01-02 15"

const (
AclPrivateXml = `<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Owner>
<ID>hehehehe</ID>
</Owner>
<AccessControlList>
<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
<ID>hehehehe</ID>
</Grantee>
<Permission>FULL_CONTROL</Permission>
</Grant>
</AccessControlList>
</AccessControlPolicy>`

AclPublicXml = `<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
AclPublicXmlPlugin = `<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Owner>
<ID>hehehehe</ID>
</Owner>
Expand Down Expand Up @@ -68,7 +54,7 @@ func Test_PluginJudge(t *testing.T) {
t.Log("PutObject Success.")
url := GenTestObjectUrl(sc) + "?X-Oss-Referer=cdn"
var policy = &datatype.AccessControlPolicy{}
err = xml.Unmarshal([]byte(AclPublicXml), policy)
err = xml.Unmarshal([]byte(AclPublicXmlPlugin), policy)
if err != nil {
t.Fatal("PutObjectPublicAclWithXml err:", err)
}
Expand All @@ -90,7 +76,13 @@ func Test_PluginJudge(t *testing.T) {
t.Fatal("StatusCode should be STATUS_OK(200), but the code is:", statusCode)
}
t.Log("Get object value:", string(data))
filePath := "/var/log/yig/access.log"
err = os.Chdir("../..")
Path, _ := os.Getwd()
if err != nil {
t.Fatal("[ERROR] Change dir", Path, "error:", err)
return
}
filePath := Path + "/access.log"
f, err := os.Open(filePath)
if err != nil {
t.Fatal("[ERROR] Read file", filePath, "error:", err)
Expand Down