use of com.gitblit.models.UserModel in project gitblit by gitblit.
the class GitServletTest method testRefChange.
private void testRefChange(AccessPermission permission, Status expectedCreate, Status expectedDelete, Status expectedRewind) throws Exception {
final String originName = "ticgit.git";
final String forkName = "refchecks/ticgit.git";
final String workingCopy = "refchecks/ticgit-wc";
// lower access restriction on origin repository
RepositoryModel origin = repositories().getRepositoryModel(originName);
origin.accessRestriction = AccessRestrictionType.NONE;
repositories().updateRepositoryModel(origin.name, origin, false);
UserModel user = getUser();
delete(user);
CredentialsProvider cp = new UsernamePasswordCredentialsProvider(user.username, user.password);
// fork from original to a temporary bare repo
File refChecks = new File(GitBlitSuite.REPOSITORIES, forkName);
if (refChecks.exists()) {
FileUtils.delete(refChecks, FileUtils.RECURSIVE);
}
CloneCommand clone = Git.cloneRepository();
clone.setURI(url + "/" + originName);
clone.setDirectory(refChecks);
clone.setBare(true);
clone.setCloneAllBranches(true);
clone.setCredentialsProvider(cp);
GitBlitSuite.close(clone.call());
// elevate repository to clone permission
RepositoryModel model = repositories().getRepositoryModel(forkName);
switch(permission) {
case VIEW:
model.accessRestriction = AccessRestrictionType.CLONE;
break;
case CLONE:
model.accessRestriction = AccessRestrictionType.CLONE;
break;
default:
model.accessRestriction = AccessRestrictionType.PUSH;
}
model.authorizationControl = AuthorizationControl.NAMED;
// grant user specified
user.setRepositoryPermission(model.name, permission);
gitblit().addUser(user);
repositories().updateRepositoryModel(model.name, model, false);
// clone temp bare repo to working copy
File local = new File(GitBlitSuite.REPOSITORIES, workingCopy);
if (local.exists()) {
FileUtils.delete(local, FileUtils.RECURSIVE);
}
clone = Git.cloneRepository();
clone.setURI(MessageFormat.format("{0}/{1}", url, model.name));
clone.setDirectory(local);
clone.setBare(false);
clone.setCloneAllBranches(true);
clone.setCredentialsProvider(cp);
try {
GitBlitSuite.close(clone.call());
} catch (GitAPIException e) {
if (permission.atLeast(AccessPermission.CLONE)) {
throw e;
} else {
// close serving repository
GitBlitSuite.close(refChecks);
// user does not have clone permission
assertTrue(e.getMessage(), e.getMessage().contains("not permitted"));
return;
}
}
Git git = Git.open(local);
// commit a file and push it
File file = new File(local, "PUSHCHK");
OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
BufferedWriter w = new BufferedWriter(os);
w.write("// " + new Date().toString() + "\n");
w.close();
git.add().addFilepattern(file.getName()).call();
git.commit().setMessage("push test").call();
Iterable<PushResult> results = null;
try {
results = git.push().setCredentialsProvider(cp).setRemote("origin").call();
} catch (GitAPIException e) {
if (permission.atLeast(AccessPermission.PUSH)) {
throw e;
} else {
// close serving repository
GitBlitSuite.close(refChecks);
// user does not have push permission
assertTrue(e.getMessage(), e.getMessage().contains("not permitted"));
GitBlitSuite.close(git);
return;
}
}
for (PushResult result : results) {
RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/master");
Status status = ref.getStatus();
if (permission.atLeast(AccessPermission.PUSH)) {
assertTrue("User failed to push commit?! " + status.name(), Status.OK.equals(status));
} else {
// close serving repository
GitBlitSuite.close(refChecks);
assertTrue("User was able to push commit! " + status.name(), Status.REJECTED_OTHER_REASON.equals(status));
GitBlitSuite.close(git);
// skip delete test
return;
}
}
// create a local branch and push the new branch back to the origin
git.branchCreate().setName("protectme").call();
RefSpec refSpec = new RefSpec("refs/heads/protectme:refs/heads/protectme");
results = git.push().setCredentialsProvider(cp).setRefSpecs(refSpec).setRemote("origin").call();
for (PushResult result : results) {
RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/protectme");
Status status = ref.getStatus();
if (Status.OK.equals(expectedCreate)) {
assertTrue("User failed to push creation?! " + status.name(), status.equals(expectedCreate));
} else {
// close serving repository
GitBlitSuite.close(refChecks);
assertTrue("User was able to push ref creation! " + status.name(), status.equals(expectedCreate));
GitBlitSuite.close(git);
// skip delete test
return;
}
}
// delete the branch locally
git.branchDelete().setBranchNames("protectme").call();
// push a delete ref command
refSpec = new RefSpec(":refs/heads/protectme");
results = git.push().setCredentialsProvider(cp).setRefSpecs(refSpec).setRemote("origin").call();
for (PushResult result : results) {
RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/protectme");
Status status = ref.getStatus();
if (Status.OK.equals(expectedDelete)) {
assertTrue("User failed to push ref deletion?! " + status.name(), status.equals(Status.OK));
} else {
// close serving repository
GitBlitSuite.close(refChecks);
assertTrue("User was able to push ref deletion?! " + status.name(), status.equals(expectedDelete));
GitBlitSuite.close(git);
// skip rewind test
return;
}
}
// rewind master by two commits
git.reset().setRef("HEAD~2").setMode(ResetType.HARD).call();
// commit a change on this detached HEAD
file = new File(local, "REWINDCHK");
os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
w = new BufferedWriter(os);
w.write("// " + new Date().toString() + "\n");
w.close();
git.add().addFilepattern(file.getName()).call();
RevCommit commit = git.commit().setMessage("rewind master and new commit").call();
// Reset master to our new commit now we our local branch tip is no longer
// upstream of the remote branch tip. It is an alternate tip of the branch.
JGitUtils.setBranchRef(git.getRepository(), "refs/heads/master", commit.getName());
// Try pushing our new tip to the origin.
// This requires the server to "rewind" it's master branch and update it
// to point to our alternate tip. This leaves the original master tip
// unreferenced.
results = git.push().setCredentialsProvider(cp).setRemote("origin").setForce(true).call();
for (PushResult result : results) {
RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/master");
Status status = ref.getStatus();
if (Status.OK.equals(expectedRewind)) {
assertTrue("User failed to rewind master?! " + status.name(), status.equals(expectedRewind));
} else {
assertTrue("User was able to rewind master?! " + status.name(), status.equals(expectedRewind));
}
}
GitBlitSuite.close(git);
// close serving repository
GitBlitSuite.close(refChecks);
delete(user);
}
use of com.gitblit.models.UserModel in project gitblit by gitblit.
the class GroovyScriptTest method test.
private void test(String script, MockGitblit gitblit, MockLogger logger, MockClientLogger clientLogger, List<ReceiveCommand> commands, RepositoryModel repository) throws Exception {
UserModel user = new UserModel("mock");
String gitblitUrl = GitBlitSuite.url;
File groovyDir = repositories().getHooksFolder();
GroovyScriptEngine gse = new GroovyScriptEngine(groovyDir.getAbsolutePath());
Binding binding = new Binding();
binding.setVariable("gitblit", gitblit);
binding.setVariable("repository", repository);
binding.setVariable("user", user);
binding.setVariable("commands", commands);
binding.setVariable("url", gitblitUrl);
binding.setVariable("logger", logger);
binding.setVariable("clientLogger", clientLogger);
Object result = gse.run(script, binding);
if (result instanceof Boolean) {
if (!((Boolean) result)) {
throw new GitBlitException(MessageFormat.format("Groovy script {0} has failed! Hook scripts aborted.", script));
}
}
}
use of com.gitblit.models.UserModel in project gitblit by gitblit.
the class HtpasswdAuthenticationTest method testAuthenticationMangerDenied.
@Test
public void testAuthenticationMangerDenied() {
UserModel user = null;
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "true");
user = auth.authenticate("user1", "".toCharArray(), null);
assertNull("User 'user1' falsely authenticated.", user);
user = auth.authenticate("user1", "pass2".toCharArray(), null);
assertNull("User 'user1' falsely authenticated.", user);
user = auth.authenticate("user2", "lalala".toCharArray(), null);
assertNull("User 'user2' falsely authenticated.", user);
user = auth.authenticate("user3", "disabled".toCharArray(), null);
assertNull("User 'user3' falsely authenticated.", user);
user = auth.authenticate("user4", "disabled".toCharArray(), null);
assertNull("User 'user4' falsely authenticated.", user);
user = auth.authenticate("plain", "text".toCharArray(), null);
assertNull("User 'plain' falsely authenticated.", user);
user = auth.authenticate("plain", "password".toCharArray(), null);
assertNull("User 'plain' falsely authenticated.", user);
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "false");
user = auth.authenticate("crypt", "".toCharArray(), null);
assertNull("User 'cyrpt' falsely authenticated.", user);
user = auth.authenticate("crypt", "passwd".toCharArray(), null);
assertNull("User 'crypt' falsely authenticated.", user);
user = auth.authenticate("md5", "".toCharArray(), null);
assertNull("User 'md5' falsely authenticated.", user);
user = auth.authenticate("md5", "pwd".toCharArray(), null);
assertNull("User 'md5' falsely authenticated.", user);
user = auth.authenticate("sha", "".toCharArray(), null);
assertNull("User 'sha' falsely authenticated.", user);
user = auth.authenticate("sha", "letmein".toCharArray(), null);
assertNull("User 'sha' falsely authenticated.", user);
user = auth.authenticate(" tabbed", "frontAndBack".toCharArray(), null);
assertNull("User 'tabbed' falsely authenticated.", user);
user = auth.authenticate(" leading", "whitespace".toCharArray(), null);
assertNull("User 'leading' falsely authenticated.", user);
}
use of com.gitblit.models.UserModel in project gitblit by gitblit.
the class HtpasswdAuthenticationTest method testAuthenticate.
@Test
public void testAuthenticate() {
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "true");
UserModel user = htpasswd.authenticate("user1", "pass1".toCharArray());
assertNotNull(user);
assertEquals("user1", user.username);
user = htpasswd.authenticate("user2", "pass2".toCharArray());
assertNotNull(user);
assertEquals("user2", user.username);
// Test different encryptions
user = htpasswd.authenticate("plain", "passWord".toCharArray());
assertNotNull(user);
assertEquals("plain", user.username);
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "false");
user = htpasswd.authenticate("crypt", "password".toCharArray());
assertNotNull(user);
assertEquals("crypt", user.username);
user = htpasswd.authenticate("md5", "password".toCharArray());
assertNotNull(user);
assertEquals("md5", user.username);
user = htpasswd.authenticate("sha", "password".toCharArray());
assertNotNull(user);
assertEquals("sha", user.username);
// Test leading and trailing whitespace
user = htpasswd.authenticate("trailing", "whitespace".toCharArray());
assertNotNull(user);
assertEquals("trailing", user.username);
user = htpasswd.authenticate("tabbed", "frontAndBack".toCharArray());
assertNotNull(user);
assertEquals("tabbed", user.username);
user = htpasswd.authenticate("leading", "whitespace".toCharArray());
assertNotNull(user);
assertEquals("leading", user.username);
}
use of com.gitblit.models.UserModel in project gitblit by gitblit.
the class SshKeysPanel method onInitialize.
@Override
protected void onInitialize() {
super.onInitialize();
setOutputMarkupId(true);
final List<SshKey> keys = new ArrayList<SshKey>(app().keys().getKeys(user.username));
final ListDataProvider<SshKey> dp = new ListDataProvider<SshKey>(keys);
final DataView<SshKey> keysView = new DataView<SshKey>("keys", dp) {
private static final long serialVersionUID = 1L;
@Override
public void populateItem(final Item<SshKey> item) {
final SshKey key = item.getModelObject();
item.add(new Label("comment", key.getComment()));
item.add(new Label("fingerprint", key.getFingerprint()));
item.add(new Label("permission", key.getPermission().toString()));
item.add(new Label("algorithm", key.getAlgorithm()));
AjaxLink<Void> delete = new AjaxLink<Void>("delete") {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
if (app().keys().removeKey(user.username, key)) {
// reset the keys list
keys.clear();
keys.addAll(app().keys().getKeys(user.username));
// update the panel
target.addComponent(SshKeysPanel.this);
}
}
};
if (!canWriteKeys) {
delete.setVisibilityAllowed(false);
}
item.add(delete);
}
};
add(keysView);
Form<Void> addKeyForm = new Form<Void>("addKeyForm");
final IModel<String> keyData = Model.of("");
addKeyForm.add(new TextAreaOption("addKeyData", getString("gb.key"), null, "span5", keyData));
final IModel<AccessPermission> keyPermission = Model.of(AccessPermission.PUSH);
addKeyForm.add(new ChoiceOption<AccessPermission>("addKeyPermission", getString("gb.permission"), getString("gb.sshKeyPermissionDescription"), keyPermission, Arrays.asList(AccessPermission.SSHPERMISSIONS)));
final IModel<String> keyComment = Model.of("");
addKeyForm.add(new TextOption("addKeyComment", getString("gb.comment"), getString("gb.sshKeyCommentDescription"), "span5", keyComment));
addKeyForm.add(new AjaxButton("addKeyButton") {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
UserModel user = GitBlitWebSession.get().getUser();
String data = keyData.getObject();
if (StringUtils.isEmpty(data)) {
// do not submit empty key
return;
}
SshKey key = new SshKey(data);
try {
key.getPublicKey();
} catch (Exception e) {
// failed to parse the key
return;
}
AccessPermission permission = keyPermission.getObject();
key.setPermission(permission);
String comment = keyComment.getObject();
if (!StringUtils.isEmpty(comment)) {
key.setComment(comment);
}
if (app().keys().addKey(user.username, key)) {
// reset add key fields
keyData.setObject("");
keyPermission.setObject(AccessPermission.PUSH);
keyComment.setObject("");
// reset the keys list
keys.clear();
keys.addAll(app().keys().getKeys(user.username));
// update the panel
target.addComponent(SshKeysPanel.this);
}
}
});
if (!canWriteKeys) {
addKeyForm.setVisibilityAllowed(false);
}
add(addKeyForm);
}
Aggregations