Skip to content

Commit

Permalink
Rename policy.PolicyRepository -> policy.Repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Waldz committed Jan 27, 2020
1 parent 4e680b0 commit 765225a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cmd/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type Dependencies struct {
IPResolver ip.Resolver
LocationResolver *location.Cache

PolicyRepository *policy.PolicyRepository
PolicyRepository *policy.Repository

StatisticsTracker *statistics.SessionStatisticsTracker
StatisticsReporter *statistics.SessionStatisticsReporter
Expand Down
2 changes: 1 addition & 1 deletion cmd/di_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (di *Dependencies) bootstrapServiceComponents(nodeOptions node.Options, ser
}
di.ServiceSessionStorage = storage

di.PolicyRepository = policy.NewPolicyRepository(di.HTTPClient, servicesOptions.AccessPolicyAddress, servicesOptions.AccessPolicyFetchInterval)
di.PolicyRepository = policy.NewRepository(di.HTTPClient, servicesOptions.AccessPolicyAddress, servicesOptions.AccessPolicyFetchInterval)
di.PolicyRepository.Start()

newDialogWaiter := func(providerID identity.Identity, serviceType string, policies *[]market.AccessPolicy) (communication.DialogWaiter, error) {
Expand Down
2 changes: 1 addition & 1 deletion core/policy/identity_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

// ValidateAllowedIdentity checks if given identity is allowed by given policies
func ValidateAllowedIdentity(repository *PolicyRepository, policies *[]market.AccessPolicy) func(identity.Identity) error {
func ValidateAllowedIdentity(repository *Repository, policies *[]market.AccessPolicy) func(identity.Identity) error {
return func(peerID identity.Identity) error {
if policies == nil {
return nil
Expand Down
40 changes: 20 additions & 20 deletions core/policy/policy_repository.go → core/policy/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ type policyMetadata struct {
rules market.AccessPolicyRuleSet
}

// PolicyRepository represents async policy fetcher from TrustOracle
type PolicyRepository struct {
// Repository represents async policy fetcher from TrustOracle
type Repository struct {
client *requests.HTTPClient

policyURL string
policyLock sync.Mutex
policyLock sync.RWMutex
policyList []policyMetadata

fetchInterval time.Duration
fetchShutdown chan struct{}
}

// NewPolicyRepository create instance of policy repository
func NewPolicyRepository(client *requests.HTTPClient, policyURL string, interval time.Duration) *PolicyRepository {
return &PolicyRepository{
// NewRepository create instance of policy repository
func NewRepository(client *requests.HTTPClient, policyURL string, interval time.Duration) *Repository {
return &Repository{
client: client,
policyURL: policyURL,
policyList: make([]policyMetadata, 0),
Expand All @@ -61,17 +61,17 @@ func NewPolicyRepository(client *requests.HTTPClient, policyURL string, interval
}

// Start begins fetching policies to repository
func (pr *PolicyRepository) Start() {
func (pr *Repository) Start() {
go pr.fetchLoop()
}

// Stop ends fetching policies to repository
func (pr *PolicyRepository) Stop() {
func (pr *Repository) Stop() {
pr.fetchShutdown <- struct{}{}
}

// Policy converts given value to valid policy rule
func (pr *PolicyRepository) Policy(policyID string) market.AccessPolicy {
func (pr *Repository) Policy(policyID string) market.AccessPolicy {
policyURL := pr.policyURL
if !strings.HasSuffix(policyURL, "/") {
policyURL += "/"
Expand All @@ -83,7 +83,7 @@ func (pr *PolicyRepository) Policy(policyID string) market.AccessPolicy {
}

// Policies converts given values to list of valid policies
func (pr *PolicyRepository) Policies(policyIDs []string) []market.AccessPolicy {
func (pr *Repository) Policies(policyIDs []string) []market.AccessPolicy {
policies := make([]market.AccessPolicy, len(policyIDs))
for i, policyID := range policyIDs {
policies[i] = pr.Policy(policyID)
Expand All @@ -92,7 +92,7 @@ func (pr *PolicyRepository) Policies(policyIDs []string) []market.AccessPolicy {
}

// AddPolicies adds given policy to repository. Also syncs policy rules from TrustOracle
func (pr *PolicyRepository) AddPolicies(policies []market.AccessPolicy) error {
func (pr *Repository) AddPolicies(policies []market.AccessPolicy) error {
pr.policyLock.Lock()
defer pr.policyLock.Unlock()

Expand All @@ -117,9 +117,9 @@ func (pr *PolicyRepository) AddPolicies(policies []market.AccessPolicy) error {
}

// RulesForPolicy gives rules of given policy
func (pr *PolicyRepository) RulesForPolicy(policy market.AccessPolicy) (market.AccessPolicyRuleSet, error) {
pr.policyLock.Lock()
defer pr.policyLock.Unlock()
func (pr *Repository) RulesForPolicy(policy market.AccessPolicy) (market.AccessPolicyRuleSet, error) {
pr.policyLock.RLock()
defer pr.policyLock.RUnlock()

index, exist := pr.getPolicyIndex(pr.policyList, policy)
if !exist {
Expand All @@ -130,9 +130,9 @@ func (pr *PolicyRepository) RulesForPolicy(policy market.AccessPolicy) (market.A
}

// RulesForPolicies gives list of rules of given policies
func (pr *PolicyRepository) RulesForPolicies(policies []market.AccessPolicy) ([]market.AccessPolicyRuleSet, error) {
pr.policyLock.Lock()
defer pr.policyLock.Unlock()
func (pr *Repository) RulesForPolicies(policies []market.AccessPolicy) ([]market.AccessPolicyRuleSet, error) {
pr.policyLock.RLock()
defer pr.policyLock.RUnlock()

policiesRules := make([]market.AccessPolicyRuleSet, len(policies))
for i, policy := range policies {
Expand All @@ -145,7 +145,7 @@ func (pr *PolicyRepository) RulesForPolicies(policies []market.AccessPolicy) ([]
return policiesRules, nil
}

func (pr *PolicyRepository) getPolicyIndex(policyList []policyMetadata, policy market.AccessPolicy) (int, bool) {
func (pr *Repository) getPolicyIndex(policyList []policyMetadata, policy market.AccessPolicy) (int, bool) {
for index, policyMeta := range policyList {
if policyMeta.policy == policy {
return index, true
Expand All @@ -155,7 +155,7 @@ func (pr *PolicyRepository) getPolicyIndex(policyList []policyMetadata, policy m
return 0, false
}

func (pr *PolicyRepository) fetchPolicyRules(policyMeta *policyMetadata) error {
func (pr *Repository) fetchPolicyRules(policyMeta *policyMetadata) error {
req, err := requests.NewGetRequest(policyMeta.policy.Source, "", nil)
if err != nil {
return errors.Wrap(err, "failed to create policy request")
Expand Down Expand Up @@ -186,7 +186,7 @@ func (pr *PolicyRepository) fetchPolicyRules(policyMeta *policyMetadata) error {
return nil
}

func (pr *PolicyRepository) fetchLoop() {
func (pr *Repository) fetchLoop() {
for {
select {
case <-pr.fetchShutdown:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ var (
)

func Test_PolicyRepository_Policy(t *testing.T) {
repo := &PolicyRepository{policyURL: "http://policy.localhost"}
repo := &Repository{policyURL: "http://policy.localhost"}
assert.Equal(
t,
market.AccessPolicy{ID: "1", Source: "http://policy.localhost/1"},
repo.Policy("1"),
)

repo = &PolicyRepository{policyURL: "http://policy.localhost/"}
repo = &Repository{policyURL: "http://policy.localhost/"}
assert.Equal(
t,
market.AccessPolicy{ID: "2", Source: "http://policy.localhost/2"},
Expand All @@ -87,7 +87,7 @@ func Test_PolicyRepository_Policy(t *testing.T) {
}

func Test_PolicyRepository_Policies(t *testing.T) {
repo := &PolicyRepository{policyURL: "http://policy.localhost"}
repo := &Repository{policyURL: "http://policy.localhost"}
assert.Equal(
t,
[]market.AccessPolicy{
Expand All @@ -96,7 +96,7 @@ func Test_PolicyRepository_Policies(t *testing.T) {
repo.Policies([]string{"1"}),
)

repo = &PolicyRepository{policyURL: "http://policy.localhost/"}
repo = &Repository{policyURL: "http://policy.localhost/"}
assert.Equal(
t,
[]market.AccessPolicy{
Expand Down Expand Up @@ -243,24 +243,24 @@ func Test_PolicyRepository_StartSyncsPolicies(t *testing.T) {
}

func Test_PolicyRepository_StartMultipleTimes(t *testing.T) {
repo := NewPolicyRepository(requests.NewHTTPClient("0.0.0.0", time.Second), "http://policy.localhost", time.Minute)
repo := NewRepository(requests.NewHTTPClient("0.0.0.0", time.Second), "http://policy.localhost", time.Minute)
repo.Start()
repo.Stop()

repo.Start()
repo.Stop()
}

func createEmptyRepo(mockServerURL string) *PolicyRepository {
return NewPolicyRepository(
func createEmptyRepo(mockServerURL string) *Repository {
return NewRepository(
requests.NewHTTPClient("0.0.0.0", 100*time.Millisecond),
mockServerURL+"/",
time.Minute,
)
}

func createFullRepo(mockServerURL string, interval time.Duration) *PolicyRepository {
repo := NewPolicyRepository(
func createFullRepo(mockServerURL string, interval time.Duration) *Repository {
repo := NewRepository(
requests.NewHTTPClient("0.0.0.0", time.Second),
mockServerURL+"/",
interval,
Expand Down
4 changes: 2 additions & 2 deletions core/service/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewManager(
dialogHandlerFactory DialogHandlerFactory,
discoveryFactory DiscoveryFactory,
eventPublisher Publisher,
policyRepo *policy.PolicyRepository,
policyRepo *policy.Repository,
) *Manager {
return &Manager{
serviceRegistry: serviceRegistry,
Expand All @@ -95,7 +95,7 @@ type Manager struct {

discoveryFactory DiscoveryFactory
eventPublisher Publisher
policyRepo *policy.PolicyRepository
policyRepo *policy.Repository
}

// Start starts an instance of the given service type if knows one in service registry.
Expand Down
2 changes: 1 addition & 1 deletion core/service/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

var (
serviceType = "the-very-awesome-test-service-type"
mockPolicy = policy.NewPolicyRepository(requests.NewHTTPClient("0.0.0.0", requests.DefaultTimeout), "http://policy.localhost/", 1*time.Minute)
mockPolicy = policy.NewRepository(requests.NewHTTPClient("0.0.0.0", requests.DefaultTimeout), "http://policy.localhost/", 1*time.Minute)
)

func TestManager_StartRemovesServiceFromPoolIfServiceCrashes(t *testing.T) {
Expand Down

0 comments on commit 765225a

Please sign in to comment.