Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to disable sessions #3183

Merged
merged 6 commits into from
Oct 14, 2016
Merged

Allow to disable sessions #3183

merged 6 commits into from
Oct 14, 2016

Conversation

inkstak
Copy link
Contributor

@inkstak inkstak commented Oct 8, 2016

According to #3180, this pull request allows to disable session.

The pull request is not ready to be merged : the test I've added won't pass, when executed along with the full test suite.
I'm not yet familiar with rack tests and looking for isolated sessions between tests.

@mperham
Copy link
Collaborator

mperham commented Oct 8, 2016

Ideally your API is backwards compatible with < 4.2, like this comment shows. Maybe something like:

Sidekiq::Web.set :sessions, false

@badosu
Copy link
Contributor

badosu commented Oct 10, 2016

@inkstak Let me know if you need any help

@inkstak
Copy link
Contributor Author

inkstak commented Oct 10, 2016

So, you can disable sessions

Sidekiq::Web.disable :sessions

or

Sidekiq::Web.set :sessions, false

You can also customize session options:

Sidekiq::Web.set :sessions, { domain: 'all' }

(Sorry for the delay, it's crazy days)

Sidekiq::Web.new
end

after { Sidekiq::Web.enable(:sessions) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it's necessary

Copy link
Contributor

@badosu badosu Oct 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@inkstak For tests that change the state of the application I recommend changing the app directly, as you can see it is an instance of Sidekiq::Web.

You can port these methods for usage on the instance and test them here. If the implementation on the instance level works and we assume that the instance implementation is a proxy of the class one (or vice-versa) then it's fine.

If you change the class configuration it will be passed for all other tests.

Ideally we would not have the ability to mount Sidekiq::Web directly, just instances of it. But for compatibility and convenience we kept it.

See

for example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not comfortable with that. At the end it's still Sidekiq::Web to mount.
Adding instance methods adds complexity for the sole purpose to be tested.

That's my opinion. If you prefer tests with instance methods, the commit is ready.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. the commit is ready, one push behind !

Copy link
Contributor

@badosu badosu Oct 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@inkstak I agree with you, but they are not for the sole purpose of testing.

The ideal design is to have only instance methods actually, the class methods only exist to provide retrocompatibiity with the old implementation, maybe we can change that in the future, depending on how @mperham sees it.

The issue with configuring Sidekiq::Web directly is that this change will replicate to the subsequent tests, unless you undefine the constant and reload the code for each test to restore the initial state.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm always in favor of instance methods over class-level. The Sinatra/Rack API is bad in that regard. I'd much prefer to see something like:

web = Sidekiq::Web.new
web.something = ...
run web

Copy link
Contributor

@badosu badosu Oct 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mperham It's up to you if you want to introduce this change, my initial design prioritized retrocompatibility so that users of the old Sinatra API would'nt suffer with the migration.

We can discuss this in a new issue if you want.

Also, this likely could trigger a major version bump as it is a big (somewhat) breaking change, even if the fix is simple.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@inkstak Feel free to push, and thanks for all the attention.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you are right. Compatibility is #1. But that doesn't mean we can't have instance methods for testing.

@@ -173,7 +173,7 @@ def display_args(args, truncate_after_chars = 2000)
end

def csrf_tag
"<input type='hidden' name='authenticity_token' value='#{session[:csrf]}'/>"
"<input type='hidden' name='authenticity_token' value='#{session[:csrf]}'/>" if session
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this should ever be disabled. Can you explain why this is necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test artefact, removed.

@mperham
Copy link
Collaborator

mperham commented Oct 10, 2016

Looking much better, let me know when you consider the PR complete.

@inkstak
Copy link
Contributor Author

inkstak commented Oct 10, 2016

The PR looks great for me, the last question is about refactoring the tests or not.


@sessions
end

Copy link
Contributor Author

@inkstak inkstak Oct 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to add settings on instance level, which inherits from class,
you can use that :

%w(sessions session_secret app_url redis_pool).each do |method_name|
  attr_writer method_name

  define_method(method_name) do
    var = "@#{method_name}"

    unless instance_variable_defined?(var)
      value = self.class.send(method_name)
      value = value.to_hash.dup if value.respond_to?(:to_hash)
      instance_variable_set(var, value)
    end

    instance_variable_get(var)
  end
end

@mperham
Copy link
Collaborator

mperham commented Oct 12, 2016

This looks great - I'm inclined to merge as is. Any other opinions?

@inkstak
Copy link
Contributor Author

inkstak commented Oct 14, 2016

I'm waiting for it. Thanks for your support !

@mperham mperham merged commit f53c59f into sidekiq:master Oct 14, 2016
@badosu
Copy link
Contributor

badosu commented Oct 14, 2016

🎉 @inkstak Thanks!

@mperham
Copy link
Collaborator

mperham commented Oct 14, 2016

This has the side effect that if sessions are disabled, CSRF protection is disabled. Users should not be able to disable rack-protection.

@mperham
Copy link
Collaborator

mperham commented Oct 14, 2016

One more issue: subclasses of Sidekiq::Web are not inheriting the value set for sessions, etc.

@devlinzed
Copy link

This should be in the FAQ

@mperham
Copy link
Collaborator

mperham commented Jul 25, 2017 via email

tnir pushed a commit to tnir/gitlabhq that referenced this pull request Sep 5, 2018
GitLab already has its own session store, so this extra Sidekiq session is
unnecessary.  In addition, the GitLab session store properly sets the Secure
flag, unlike the default Rack session.

CSRF protection in the Sidekiq /admin page continues to work with the existing
GitLab session.

See sidekiq/sidekiq#3183 for more details.

Part of #49120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants