Skip to content

Commit daf2233

Browse files
committed
Implement log access settings
1 parent a8815e4 commit daf2233

File tree

7 files changed

+30
-5
lines changed

7 files changed

+30
-5
lines changed

lib/travis.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class GithubApiError < StandardError; end
3131
class AdminMissing < StandardError; end
3232
class RepositoryMissing < StandardError; end
3333
class LogAlreadyRemoved < StandardError; end
34+
class LogExpired < StandardError; end
35+
class LogAccessDenied < StandardError; end
3436
class AuthorizationDenied < StandardError; end
3537
class JobUnfinished < StandardError; end
3638

lib/travis/api/v3.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def location(env)
3939
JobNotCancelable = ClientError .create('job is not running, cannot cancel', status: 409)
4040
JobUnfinished = ClientError .create('job still running, cannot remove log yet', status: 409)
4141
LogAlreadyRemoved = ClientError .create('log has already been removed', status: 409)
42+
LogExpired = ClientError .create("We're sorry, but this data is not available anymore. Please check the repository settings in Travis CI.", status: 403)
43+
LogAccessDenied = ClientError .create("We're sorry, but this data is not available. Please check the repository settings in Travis CI.", status: 403)
4244
LoginRequired = ClientError .create('login required', status: 403)
4345
MethodNotAllowed = ClientError .create('method not allowed', status: 405)
4446
NotImplemented = ServerError .create('request not (yet) implemented', status: 501)

lib/travis/api/v3/models/user_settings.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class Models::UserSettings < Models::JsonSlice
1212
attribute :config_validation, Boolean, default: lambda { |us, _| us.config_validation? }
1313
attribute :share_encrypted_env_with_forks, Boolean, default: false
1414
attribute :share_ssh_keys_with_forks, Boolean, default: lambda { |us, _| us.share_ssh_keys_with_forks? }
15+
attribute :job_log_time_based_limit, Boolean, default: lambda { |s, _| s.job_log_access_permissions[:time_based_limit] }
16+
attribute :job_log_access_based_limit, Boolean, default: lambda { |s, _| s.job_log_access_permissions[:access_based_limit] }
17+
attribute :job_log_access_older_than_days, Integer, default: lambda { |s, _| s.job_log_access_permissions[:older_than_days] }
1518

1619
attr_reader :repo
1720

@@ -57,5 +60,9 @@ def cutoff_date
5760
def days_since_jan_15
5861
Date.today.mjd - JAN_15.mjd + 1
5962
end
63+
64+
def job_log_access_permissions
65+
Travis.config.to_h.fetch(:job_log_access_permissions) { {} }
66+
end
6067
end
6168
end

lib/travis/api/v3/queries/log.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
module Travis::API::V3
22
class Queries::Log < RemoteQuery
3-
def find_by_job_id(job_id)
4-
find Models::Job.find(job_id)
3+
def find_by_job_id(repo_can_write, job_id)
4+
find repo_can_write, Models::Job.find(job_id)
55
end
66

7-
def find(job)
7+
def find(repo_can_write, job)
88
@job = job
9+
raise LogExpired if !job.repository.user_settings.job_log_time_based_limit && job.started_at < Time.now - job.repository.user_settings.job_log_access_older_than_days.days
10+
raise LogAccessDenied if job.repository.user_settings.job_log_access_based_limit && !repo_can_write
11+
912
remote_log = Travis::RemoteLog::Remote.new(platform: platform).find_by_job_id(platform_job_id)
1013
raise EntityMissing, 'log not found'.freeze if remote_log.nil?
1114
log = Travis::API::V3::Models::Log.new(remote_log: remote_log, job: job)

lib/travis/api/v3/services/log/find.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ class Services::Log::Find < Service
33
params 'log.token'
44

55
def run!
6-
log = query.find_by_job_id(params['job.id'])
6+
job = Models::Job.find(params['job.id'])
7+
repo_can_write = !!job.repository.users.where(id: access_control.user.id, permissions: { push: true }).first
8+
9+
log = query.find(repo_can_write, job)
710
raise(NotFound, :log) unless access_control.visible? log
811
result log
912
end

lib/travis/config/defaults.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ def fallback_logs_api_auth_token
8888
force_authentication: false,
8989
yml: { url: 'https://yml.travis-ci.org', token: 'secret', auth_key: 'abc123' },
9090
read_only: ENV['READ_ONLY'] || false,
91-
vcs: {}
91+
vcs: {},
92+
job_log_access_permissions: { time_based_limit: false, access_based_limit: false, older_than_days: 365, max_days_value: 730, min_days_value: 30 }
9293

9394
default :_access => [:key]
9495

lib/travis/model/repository/settings.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,20 @@ def custom_timeouts?(settings)
101101
attribute :allow_config_imports, Boolean, default: false
102102
attribute :share_encrypted_env_with_forks, Boolean, default: false
103103
attribute :share_ssh_keys_with_forks, Boolean, default: nil
104+
attribute :job_log_time_based_limit, Boolean, default: lambda { |s, _| s.job_log_access_permissions[:time_based_limit] }
105+
attribute :job_log_access_based_limit, Boolean, default: lambda { |s, _| s.job_log_access_permissions[:access_based_limit] }
106+
attribute :job_log_access_older_than_days, Integer, default: lambda { |s, _| s.job_log_access_permissions[:older_than_days] }
104107

105108
validates :maximum_number_of_builds, numericality: true
106109

107110
validate :api_builds_rate_limit_restriction
108111

109112
validates_with TimeoutsValidator
110113

114+
def job_log_access_permissions
115+
Travis.config.to_h.fetch(:job_log_access_permissions) { {} }
116+
end
117+
111118
def auto_cancel_default?
112119
ENV.fetch('AUTO_CANCEL_DEFAULT', 'false') == 'true'
113120
end

0 commit comments

Comments
 (0)